Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate 4 - getGeneratedKeys() support is not enabled

We are in a mode of upgrading hibernate from version 3.6 to 4.1.9 and I am facing an error

[#|2013-01-14T18:33:46.519+0530|INFO|glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=122;_ThreadName=Thread-3;|2013-01-14 18:33:46,518 [http-thread-pool-8080(5)] ERROR envId{} - sesId{} - com.spmsoftware.webframework.exception.ExceptionHandlerUtil[31]-Unhandled exception! org.hibernate.AssertionFailure: getGeneratedKeys() support is not enabled at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.checkAutoGeneratedKeysSupportEnabled(StatementPreparerImpl.java:93) ~[hibernate-core-4.1.9.Final.jar:4.1.9.Final] at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareStatement(StatementPreparerImpl.java:112) ~[hibernate-core-4.1.9.Final.jar:4.1.9.Final] at org.hibernate.id.SequenceIdentityGenerator$Delegate.prepare(SequenceIdentityGenerator.java:106) ~[hibernate-core-4.1.9.Final.jar:4.1.9.Final] at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:55) ~[hibernate-core-4.1.9.Final.jar:4.1.9.Final] at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2936) ~[hibernate-core-4.1.9.Final.jar:4.1.9.Final] at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3447) ~[hibernate-core-4.1.9.Final.jar:4.1.9.Final] at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81) ~[hibernate-core-4.1.9.Final.jar:4.1.9.Final] at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362) ~[hibernate-core-4.1.9.Final.jar:4.1.9.Final] at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:203) ~[hibernate-core-4.1.9.Final.jar:4.1.9.Final] at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:183) ~[hibernate-core-4.1.9.Final.jar:4.1.9.Final] at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:167) ~[hibernate-core-4.1.9.Final.jar:4.1.9.Final] at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:320) ~[hibernate-core-4.1.9.Final.jar:4.1.9.Final] at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:287) ~[hibernate-core-4.1.9.Final.jar:4.1.9.Final] at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:193) ~[hibernate-core-4.1.9.Final.jar:4.1.9.Final] at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:126) ~[hibernate-core-4.1.9.Final.jar:4.1.9.Final] at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:78) ~[hibernate-entitymanager-4.1.9.Final.jar:4.1.9.Final] at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:208) ~[hibernate-core-4.1.9.Final.jar:4.1.9.Final] at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:151) ~[hibernate-core-4.1.9.Final.jar:4.1.9.Final] at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:78) ~[hibernate-core-4.1.9.Final.jar:4.1.9.Final] at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:843) ~[hibernate-core-4.1.9.Final.jar:4.1.9.Final] at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:818) ~[hibernate-core-4.1.9.Final.jar:4.1.9.Final] at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:822) ~[hibernate-core-4.1.9.Final.jar:4.1.9.Final] at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:865) ~[hibernate-entitymanager-4.1.9.Final.jar:4.1.9.Final]

This error is seen when I try to persist a hibernate entity which has the following definition

@Entity
@Table(name = "ACCESS")
@GenericGenerator(name = "auth-generator",
    strategy = "sequence-identity",
    parameters = @org.hibernate.annotations.Parameter(name = "sequence", value = "ACCESS_SEQ"))
 public class Access implements Identifiable {
    private Long id;

   @Id
   @GeneratedValue(generator = "auth-generator")
   @Column(name = "ROW_IDENTIFIER", nullable = false)
    public Long getId() {
        return id;
    }
 }

On comparing hibernate source code for 3.6 I saw that while creating a prepared statement for SequenceIdentityGenerator hibernate does NOT check for "hibernate.jdbc.use_get_generated_keys" (AbstractBatcher - 506) and for version 4.1.9 it does (StatementPreparerImpl - 112).

Due to this, our application fails on upgrade. Any idea on what is the reason behind this change?

like image 406
Andy Dufresne Avatar asked Jan 15 '13 13:01

Andy Dufresne


1 Answers

I was stuck in the same issue. Finally found a way out. put this line in your hibernate.cfg.xml file.

<property name="hibernate.jdbc.use_get_generated_keys">true</property>

And the reference and explanation for that can be found in :

http://www.jarvana.com/jarvana/view/hibernate/hibernate/3.0.2/hibernate-3.0.2-sources.jar!/org/hibernate/jdbc/AbstractBatcher.java?format=ok

like image 95
LPD Avatar answered Nov 03 '22 08:11

LPD