Hibernate 5.0.0.CR2 中未启用 getGeneratedKeys() 支持错误

getGeneratedKeys() support is not enabled error in Hibernate 5.0.0.CR2

我正在尝试连接到 Oracle,但我在 Hibernate-5.0 中收到此错误,即使我已在配置中启用此 属性。

> Aug 10, 2015 8:49:34 AM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
Aug 10, 2015 8:49:35 AM org.hibernate.AssertionFailure <init>
ERROR: HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): org.hibernate.AssertionFailure: getGeneratedKeys() support is not enabled
Exception in thread "main" org.hibernate.AssertionFailure: getGeneratedKeys() support is not enabled
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.checkAutoGeneratedKeysSupportEnabled(StatementPreparerImpl.java:94)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareStatement(StatementPreparerImpl.java:113)
    at org.hibernate.id.SequenceIdentityGenerator$Delegate.prepare(SequenceIdentityGenerator.java:93)

我的hibernate.cfg.xml如下

<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <property name="hibernate.connection.password">*****</property>
    <property name="hibernate.connection.url">jdbc:oracle:thin:@*****</property>
    <property name="hibernate.connection.username">*****</property>
    <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
    <property name="hibernate.jdbc.use_get_generated_keys">true</property>

    <property name="show_sql">true</property>
    <property name="connection.pool_size">1</property>
    <!-- List of XML mapping files -->
    <mapping resource="mapping/Employee.hbm.xml" />
</session-factory>

而Employee.hbm.xml如下所示

<hibernate-mapping>
<class name="domain.Employee" table="EMPLOYEE">
    <id name="id" type="integer" column="id">
        <generator class="sequence-identity">
            <param name="sequence">EMP_SEQ</param>
        </generator>
    </id>
    <property name="firstName" column="first_name" type="string" />
    <property name="lastName" column="last_name" type="string" />
    <property name="salary" column="salary" type="integer" />
</class>

试了很多次好像都解决不了。我哪里错了?任何指针都是一个很大的帮助。提前致谢。

我不认为问题出在您的代码上,而是 Oracle。

根据 Oracle9Dialect 中的评论,Oracle 不支持返回生成的密钥,与其他数据库不同。

不确定这是否是 Hibernate 版本的问题,因为当我将依赖版本从 POM 中的 5.0.0.CR2 更改为 4.3.9.Final 时它工作正常。

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.9.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.9.Final</version>
    </dependency>