Search code examples
javamysqlhibernateentity

Auto Increment not working for Entity class with composite Key


I want one of my Composite key elements to be auto incremented and I am using Embedded Key for my entity class and therefore I am not able to use the Generated Values Annotation to solve the problem since @GeneratedValue annotation is ignored when using @Embeddable or @EmbeddedId.


Solution

  • You cannot use @GeneratedValue with @EmbeddedId, @Embeddable or @IdClass, the @GeneratedValue annotation gets ignored. You have to manually assign the values.

    Check the following references :

    https://java.net/jira/browse/GLASSFISH-13567

    https://forums.hibernate.org/viewtopic.php?p=2393944&sid=57b20ef2c7de6f6bcba632130987bc22