Hibernate 5 转换插入问题

Posted

技术标签:

【中文标题】Hibernate 5 转换插入问题【英文标题】:Hibernate 5 conversion insert issue 【发布时间】:2019-10-29 11:48:29 【问题描述】:

我正在开发一个应用程序,我们正在从 Hibernate 3.4 转换到 5.4(以及 Spring 3.4 到 5.4)。它是一个 WLS 19.0.0.4 服务器,java 8 连接到 DB2 数据库。插入数据库时​​出现“HIBERNATE_SEQUENCE IS AN UNDEFINED NAME”错误(下面的堆栈跟踪)。它能够检索。

在 Hibernate 5 中是否存在插入或生成序列值的语法更改或我未考虑的其他更改?

我研究发现 SQLCODE=-204, SQLSTATE=42704 表示“对象不存在”,但我认为它与 IBM 无关,只是因为我能够在它是 Spring/Hibernate 时对其进行插入3.4

休眠和持久化 jar hibernate-core-5.4.2.Final.jar javax.persistence-api-2.2.jar(由于我看到的冲突,它替换了 ejb3-persistence-1.0.2.GA.jar)

示例 hbm.xml。这是我们方法的一个示例。它发生在所有插入中

  <class name="somepath.TableLog" table="TABLE_NAME">

    <id name="id" type="java.lang.Long" access="field">
        <column name="SYS_ID" />
        <generator class="sequence">
            <param name="sequence">TABLE_LOG_SEQ</param>
        </generator>
    </id>
    <property name="logEntry" type="java.lang.String" access="field">
        <column name="LOG_ENTRY" length="32000" />
    </property>
    <property name="userId" type="java.lang.String" access="field">
        <column name="USER_ID" length="2" />
    </property>

</class>

对应类 - logEntry 的 getter 和 setter 在父类中。

public class TableLog extends AbstractLog 

    private String userId = "";
    public TableLog (String logEntry,  String userId) 
        super(logEntry, userId);
    
    /**
     * Instantiates a new sys mgmt log.
     */
    public TableLog() 
        super();                
    

    public String getUserId() 
        return userId;
    

    public void setUserId(String userId) 
        this.userId = userId;
    


hibernate-hsqldb.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>

        <!-- Database connection settings -->
        <property name="hibernate.connection.datasource">some jndi</property>
        <property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WebSphereExtendedJTATransactionLookup</property>
        <property name="connection.driver_class">com.ibm.db2.jcc.DB2ConnectionPoolDataSource</property>

        <!-- <property name="hibernate.default_schema">MWKDB2MV</property> -->

        <property name="current_session_context_class">thread</property>

        <property name="hibernate.dialect">tus.persist.util.impl.Db2390DialectSequenceFix</property>

        <property name="current_session_context_class">thread</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">false</property>
        <!-- Drop and re-create the database schema on startup -->
        <!--property name="hibernate.hbm2ddl.auto">create</property-->

    </session-factory>
</hibernate-configuration>

错误堆栈跟踪:

[ERROR][Default Executor-thread-130] TableStructure$1$1: could not read a hi value
com.ibm.db2.jcc.am.vo: (schema).HIBERNATE_SEQUENCE IS AN UNDEFINED NAME. SQLCODE=-204, SQLSTATE=42704, DRIVER=4.8.86
    at com.ibm.db2.jcc.am.gd.a(gd.java:676)
    at com.ibm.db2.jcc.am.gd.a(gd.java:60)
    at com.ibm.db2.jcc.am.gd.a(gd.java:127)
    at com.ibm.db2.jcc.am.wm.c(wm.java:2510)
    at com.ibm.db2.jcc.am.wm.d(wm.java:2498)
    at com.ibm.db2.jcc.am.wm.a(wm.java:1978)
    at com.ibm.db2.jcc.am.wm.a(wm.java:1956)
    at com.ibm.db2.jcc.t4.fb.g(fb.java:135)
    at com.ibm.db2.jcc.t4.fb.a(fb.java:40)
    at com.ibm.db2.jcc.t4.t.a(t.java:32)
    at com.ibm.db2.jcc.t4.ub.i(ub.java:135)
    at com.ibm.db2.jcc.am.wm.hb(wm.java:1949)
    at com.ibm.db2.jcc.am.xm.pc(xm.java:2996)
    at com.ibm.db2.jcc.am.xm.b(xm.java:3766)
    at com.ibm.db2.jcc.am.xm.bc(xm.java:692)
    at com.ibm.db2.jcc.am.xm.executeQuery(xm.java:662)
    at com.ibm.ws.rsadapter.jdbc.WSJdbcPreparedStatement.executeQuery(WSJdbcPreparedStatement.java:476)
    at org.hibernate.id.enhanced.TableStructure.executeQuery(TableStructure.java:216)
    at org.hibernate.id.enhanced.TableStructure.access$300(TableStructure.java:46)
    at org.hibernate.id.enhanced.TableStructure$1$1.execute(TableStructure.java:138)
    at org.hibernate.id.enhanced.TableStructure$1$1.execute(TableStructure.java:126)
    at org.hibernate.jdbc.WorkExecutor.executeReturningWork(WorkExecutor.java:55)
    at org.hibernate.jdbc.AbstractReturningWork.accept(AbstractReturningWork.java:34)
    at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcIsolationDelegate.delegateWork(JdbcIsolationDelegate.java:57)
    at org.hibernate.id.enhanced.TableStructure$1.getNextValue(TableStructure.java:125)
    at org.hibernate.id.enhanced.NoopOptimizer.generate(NoopOptimizer.java:40)
    at org.hibernate.id.enhanced.SequenceStyleGenerator.generate(SequenceStyleGenerator.java:520)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:119)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:97)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
    at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:682)
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:674)
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:669)
    at sun.reflect.GeneratedMethodAccessor1359.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Method.java:508)
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:350)
    at com.sun.proxy.$Proxy62.saveOrUpdate(Unknown Source)
    at tus.persist.service.impl.PersistenceService.executeSaveOrUpdateBusinessObject(PersistenceService.java:563)
    at tus.persist.service.impl.PersistenceService.execute_aroundBody20(PersistenceService.java:447)
    at tus.persist.service.impl.PersistenceService.execute_aroundBody21$advice(PersistenceService.java:56)
    at tus.persist.service.impl.PersistenceService.execute(PersistenceService.java:1)
    at tus.persist.request.impl.SaveOrUpdateBusinessObjectRequest.execute(SaveOrUpdateBusinessObjectRequest.java:48)
    at tus.persist.request.impl.SaveOrUpdateBusinessObjectRequest.execute(SaveOrUpdateBusinessObjectRequest.java:1)
    at fw.journaling.impl.HibernateLogProcessor.execute(HibernateLogProcessor.java:80)
    at aspect.impl.AbstractAspect.processSystemLogRequestMessage(AbstractAspect.java:240)
    at aspect.impl.AbstractProdStatisticsAspect.logSessionContextMessage(AbstractProdStatisticsAspect.java:75)
    at aspect.impl.SignInSignOffAspect.processLogSessionContextMessage(SignInSignOffAspect.java:98)
    at aspect.impl.SignInSignOffAspect.startSession(SignInSignOffAspect.java:143)
    at app.context.impl.SessionContext.myInit(SessionContext.java:117)
    at sun.reflect.GeneratedMethodAccessor1357.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Method.java:508)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1903)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1846)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1774)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$1(AbstractBeanFactory.java:356)
    at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$106.0000000022E64A30.getObject(Unknown Source)
    at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:45)
    at org.springframework.web.context.request.SessionScope.get(SessionScope.java:57)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:353)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
    at org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:193)
    at com.sun.proxy.$Proxy35.getCurrentProcessContext(Unknown Source)
    at ui.handler.impl.ActionsHandler.getCurrentProcessContext(ActionsHandler.java:502)
    at ui.handler.impl.ActionsHandler.setupButtons(ActionsHandler.java:1096)
    at ui.page.BasePage.superRenderPreProcessing(BasePage.java:608)
    at sun.reflect.GeneratedMethodAccessor1351.invoke(Unknown Source)
    ...
[ WARN][Default Executor-thread-130] SqlExceptionHelper: SQL Error: -204, SQLState: 42704
[ERROR][Default Executor-thread-130] SqlExceptionHelper: (schema).HIBERNATE_SEQUENCE IS AN UNDEFINED NAME. SQLCODE=-204, SQLSTATE=42704, DRIVER=4.8.86
[ WARN][Default Executor-thread-130] SqlExceptionHelper: SQL Error: -516, SQLState: 26501
[ERROR][Default Executor-thread-130] SqlExceptionHelper: THE DESCRIBE STATEMENT DOES NOT SPECIFY A PREPARED STATEMENT. SQLCODE=-516, SQLSTATE=26501, DRIVER=4.8.86
[ WARN][Default Executor-thread-130] SqlExceptionHelper: SQL Error: -514, SQLState: 26501
[ERROR][Default Executor-thread-130] SqlExceptionHelper: THE CURSOR SQL_CURLH300C1 IS NOT IN A PREPARED STATE. SQLCODE=-514, SQLSTATE=26501, DRIVER=4.8.86
[ERROR][Default Executor-thread-130] PersistenceService: org.hibernate.exception.SQLGrammarException: error performing isolated work
[DEBUG][Default Executor-thread-130] TransactionImpl: rolling back
[DEBUG][Default Executor-thread-130] SynchronizationRegistryStandardImpl: Clearing local Synchronizations
[ERROR][Default Executor-thread-130] PersistenceService: fw.exception.impl.ApplicationException: org.hibernate.exception.SQLGrammarException: error performing isolated work

【问题讨论】:

我不知道您的问题的答案,但 SQLCODE=-204, SQLSTATE=42704 来自 Db2。我假设你运行的是同一个数据库,所以序列应该在那里。我最好的猜测是序列的模式在生成的查询中丢失了。 据我所见,您没有提及Db2版本,但是您可以尝试select seqschema from syscat.sequences where seqname = 'HIBERNATE_SEQUENCE'另一种可能性是在目录中引用了序列名称和/或seqschema,请尝试@987654328 @ 谢谢Lennart,我得检查一下db2 版本。我们使用 WAS 8.5.5 运行相同的应用程序,而我使用带有 Spring/Hibernate 3.6 的 WLS 19.0.0.1 运行它。插入适用于这两种实现。这个问题从转换到 Spring 5.1.7 /Hibernate 5.4.2 开始。感谢您提醒检查序列表。 TABLE_LOG_SEQ 的记录在那里。 我在这里找到了答案:***.com/questions/32968527/… 我认为你应该对此做一个简短的回答并接受它。接受自己的答案不会获得任何分数,但我(可能还有其他人)会投票给它,你会因此获得分数。 【参考方案1】:

我在下面的网址找到了答案。我的设置不同,因为我们使用的是序列表中的序列名称,但是更改为 hibernate.id.new_generator_mappings 的默认值的效果导致了相同的错误。 (在 Hib 3 中它是假的,在 hib 5 中它是真的)。所以答案是在hibernate.cfg.xml中加入如下属性。感谢@Eva Mariam 的回答。

<property name="hibernate.id.new_generator_mappings">false</property>

Hibernate-sequence doesn't exist

【讨论】:

以上是关于Hibernate 5 转换插入问题的主要内容,如果未能解决你的问题,请参考以下文章

Hibernae

求教第一个hibernae程序的配置文件hibernate.cfg.xml出错了,求帮忙解决,如下

hibernate

5 -- Hibernate的基本用法 --3 Hibernate的体系结构

5 -- Hibernate的基本用法 --4 9 其他常用的配置属性

Hibernaate 详解