Hibernate 4 - 未启用getGeneratedKeys()支持

时间:2013-01-15 13:08:03

标签: hibernate generator sequence

我们处于将hibernate从版本3.6升级到4.1.9的模式,我正面临一个错误

  

[#| 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] - 未处理的异常!   org.hibernate.AssertionFailure:未启用getGeneratedKeys()支持       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]       在org.hibernate.id.SequenceIdentityGenerator $ Delegate.prepare(SequenceIdentityGenerator.java:106)〜[hibernate-core-4.1.9.Final.jar:4.1.9.Final]       在org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:55)〜[hibernate-core-4.1.9.Final.jar:4.1.9.Final]       在org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2936)〜[hibernate-core-4.1.9.Final.jar:4.1.9.Final]       在org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3447)〜[hibernate-core-4.1.9.Final.jar:4.1.9.Final]       在org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:81)〜[hibernate-core-4.1.9.Final.jar:4.1.9.Final]       在org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)〜[hibernate-core-4.1.9.Final.jar:4.1.9.Final]       在org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:203)〜[hibernate-core-4.1.9.Final.jar:4.1.9.Final]       在org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:183)〜[hibernate-core-4.1.9.Final.jar:4.1.9.Final]       在org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:167)〜[hibernate-core-4.1.9.Final.jar:4.1.9.Final]       在org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:320)〜[hibernate-core-4.1.9.Final.jar:4.1.9.Final]       在org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:287)〜[hibernate-core-4.1.9.Final.jar:4.1.9.Final]       在org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:193)〜[hibernate-core-4.1.9.Final.jar:4.1.9.Final]       在org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:126)〜[hibernate-core-4.1.9.Final.jar:4.1.9.Final]       在org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:78)〜[hibernate-entitymanager-4.1.9.Final.jar:4.1.9.Final]       在org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:208)〜[hibernate-core-4.1.9.Final.jar:4.1.9.Final]       在org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:151)〜[hibernate-core-4.1.9.Final.jar:4.1.9.Final]       在org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:78)〜[hibernate-core-4.1.9.Final.jar:4.1.9.Final]       在org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:843)〜[hibernate-core-4.1.9.Final.jar:4.1.9.Final]       在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]       在org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:865)〜[hibernate-entitymanager-4.1.9.Final.jar:4.1.9.Final]

当我尝试持久化具有以下定义的hibernate实体时会出现此错误

@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;
    }
 }

在比较3.6的hibernate源代码时,我发现在为SequenceIdentityGenerator创建预准备语句时,hibernate不会检查“hibernate.jdbc.use_get_generated_keys”(AbstractBatcher - 506),而对于版本4.1.9,它会检查它(StatementPreparerImpl - 112)

因此,我们的应用程序升级失败。有关这种变化背后原因的任何想法吗?

1 个答案:

答案 0 :(得分:11)

我遇到了同样的问题。终于找到了出路。将此行放在hibernate.cfg.xml文件中。

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

可以在以下网址找到对此的参考和解释:

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