Link to home
Start Free TrialLog in
Avatar of leobaz2
leobaz2

asked on

Using @IdClass with @GeneratedValue

I am using EJB3 persistence, JBoss, MySql.  I am able to have an entity with one primary key marked with @Id where this key is an autogenerated field with the annotation @GeneratedValue(strategy=GenerationType.AUTO).  The table is created with the primary key and when persisting objects, I see the id field being incremented.

Everything works fine until I started creating entites that have composite primary keys using @IdClass where one of those keys have @GeneratedValue(strategy=GenerationType.AUTO).  I can see that the table gets created with the correct primary key.  The problem is that the primary key field that has the @GeneratedValue attribute is not being incremented; it is always 0.

I have tried different combinations of putting the @Id and @GeneratedValue attributes on the fields and getters of the entity and the primary key class but nothing works.
Avatar of Mr_It
Mr_It

What is the use of a composite PK if you want to auto-generate one of the values? Isn't a single ID property unique enough to identify the entity? I really don't understand why you would like to have a composite PK if one of its properties is uniquely generated anyway. For me it makes no sense to have something like this.

Anyway, you simply cannot put @Id annotations in classes other than an entity, because it makes no sense.  The specs say:

'The Id annotation specifies the primary key property or field of an entity. The Id annotation may be applied in an entity or mapped superclass.'

It's normal that it will be ignored in the non-entity class. That's why it's always 0...

Anyway, I understand that you might expect it to work with the IdClass thing, because the specs are unclear about this:

'The GeneratedValue annotation may be applied to a primary key property or
field of an entity or mapped superclass in conjunction with the Id annotation.'

If you have a composite PK, can we refer to one of the properties annotated with @Id as a primary key property? It's only a partial primary key property, not the primary key property itself. They should go into more detail about this...

Still, I don't see the purpose of having a composite key with one of the values unique. Maybe you are coding for an existing database?

Maybe vendor specific annotations can do the trick. I never came across a use case for this.

Good luck ;-)

Avatar of leobaz2

ASKER

I am going to have multiple clients sharing the same database but not the same data.  I originally had one primary key for every table that was auto generated.  I wanted to change it so that each primary key was composite on the auto generated column and the "client id".  This way, it would be impossible for a client to view another client's data.  Also, when I do a find I pass the client id along with the auto generate key which I liked.  

However, since I can't use composite primary keys with an auto generated field, I am doing a find on the auto generated field and then just verifying that the client id from the persisted object equals the logged on user's client id.
ASKER CERTIFIED SOLUTION
Avatar of Mr_It
Mr_It

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of leobaz2

ASKER

I actually agree with you that primary keys should not have business logic even though the logic is minimal in this case.  I'll change my logic to use a where clause.

Thanks for your help.