Saturday, January 30, 2016

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save(): com.test.TestDO
    at org.hibernate.id.Assigned.generate(Assigned.java:34)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101)
    at org.hibernate.event.internal.DefaultMergeEventListener.saveTransientEntity(DefaultMergeEventListener.java:255)
    at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:235)
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:173)
    at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:849)
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:831)
    at org.hibernate.engine.spi.CascadingActions$6.cascade(CascadingActions.java:260)
    at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:379)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:304)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:146)
    at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:412)
    at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:344)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:307)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:146)
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:95)
    at org.hibernate.event.internal.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:468)
    at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:327)
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:170)
    at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:69)
    at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:839)
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:821)
    at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:826)



Fix:

If you have specified Auto Increment for the Primary Key field, then you can specify "identity" as a generator class in hibernate config file as shown below:



<hibernate-mapping>
    <class name="com.test.service.dataobjects.TestDO" table="test">
    <id name="test" column="testId">
        <generator class="identity"/>
    </id>

....
....
</hibernate-mapping>

No comments:

Post a Comment