Using @IdClass with @GeneratedValue
Posted on 2008-10-15
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.