I’ve created my MySQL database with MySQL Workbench, then I added MySQL JDBC Driver and EclipseLink libraries and then created entity classes from database; all with NetBeans. I needed to resign from using simple “id” column names because of my views and all the merges; it was just easier to rename all of them in a way: “id” -> “TableNameId”. During the creation of entity classes, there was this message saying that I will have to manually set IDs for all the entities. How to do that? Every table have TableName.java and TableNamePK.java files.
There are such lines within the TableNamePK.java files:
@EmbeddedId
protected AdministratorsPK administratorsPK;
and such a lines within TableNamePK.java files:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "UserId", nullable = false)
private Integer userId;
I believe that should be it but I’m worried about the message – is there anything I should do to make it work?
It throws as error:
Exception Description: Entity class [class kwestionariusz.manager.View2] has no primary key specified. It should define either an @Id, @EmbeddedId or an @IdClass. If you have defined PK using any of these annotations then make sure that you do not have mixed access-type (both fields and properties annotated) in your entity class hierarchy.
I assume it’s the second case then… but how to get it right?
If your primary keys are simple integer fields, I believe all you should need to do is use the @Id annotation. As long as your field name matches the attribute name, it should just be:
I haven’t used @EmbeddedId before, but I think it’s only necessary if you have a composite PK (see the docs) for which you have to define an Embeddable class. Though without knowing your database structure it’s hard to say why NetBeans generated those classes for you.
As an aside, in my opinion naming every PK field “id” makes things simpler. To avoid complication in your views and merges, just use the AS keyword to rename it in your SELECT statement: