Hibernate 5转换插入问题

时间:2019-06-14 20:50:00

标签: hibernate

我正在开发一个将Hibernate 3.4转换为5.4(从Spring 3.4转换为5.4)的应用程序。它是WLS 19.0.0.4服务器,其中Java 8连接到DB2数据库。插入数据库时​​,它得到“ HIBERNATE_SEQUENCE是一个未定义的名称”错误(下面的堆栈跟踪)。它能够检索。

在Hibernate 5中是否没有考虑插入或生成序列值的语法更改或其他某些更改?

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

休眠和持久罐 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的获取器和设置器在父类中。

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

1 个答案:

答案 0 :(得分: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