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

时间:2015-08-10 08:09:39

标签: java hibernate orm hibernate-mapping nhibernate-configuration

我正在尝试连接到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>

我已经多次尝试但似乎没有解决。我在哪里错了?任何指针都是很有帮助的。提前谢谢。

2 个答案:

答案 0 :(得分:1)

不确定这是否是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>

答案 1 :(得分:0)

我认为这不是你的代码问题,而是甲骨文。

根据Oracle9Dialect中的评论,Oracle不支持返回生成的密钥,而不像其他任何数据库那样。