Java 冬眠和春季持续性问题。可能的标识值没有增加?

Java 冬眠和春季持续性问题。可能的标识值没有增加?,java,hibernate,spring,Java,Hibernate,Spring,我使用的是hibernate 3.2.6和spring 2.5.6 Im连接到的数据库是:DB2forzOS390V8.1 当我运行我的测试用例(如下)时,我保存测试文件对象。Hibernate确实将对象保存到数据库中,但我的测试在保存后失败,它试图用正确的id更新对象。我认为它没有获得正确的id(看起来它的id为“0”),或者我甚至不确定它为什么要尝试更新已持久化的对象。我不知道这是不是spring、hibernate、mapping、方言等问题 我的配置: 业务对象: <hiberna

我使用的是hibernate 3.2.6和spring 2.5.6 Im连接到的数据库是:DB2forzOS390V8.1

当我运行我的测试用例(如下)时,我保存测试文件对象。Hibernate确实将对象保存到数据库中,但我的测试在保存后失败,它试图用正确的id更新对象。我认为它没有获得正确的id(看起来它的id为“0”),或者我甚至不确定它为什么要尝试更新已持久化的对象。我不知道这是不是spring、hibernate、mapping、方言等问题

我的配置:

业务对象:

<hibernate-mapping>

    <class name="cat.edis.tmiweb.business.File" table="FILE">

        <id name="id" type="java.lang.Long" column="gen_file_id">
            <generator class="native"></generator>
        </id>

        <property name="creationTimeStamp" type="java.util.Date" column="crte_ts" />
        <property name="name" type="java.lang.String" column="file_nm" />
        <property name="type" type="java.lang.String" column="file_typ_desc" />
        <property name="description" type="java.lang.String" column="file_desc" />
        <property name="length" type="java.lang.Long" column="file_lgth" />
        <property name="contentBlob" type="blob" column="file_cntnt" />

    </class>

</hibernate-mapping>
应用程序上下文:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <!--  Webservices -->
    <bean id="FPSService" class="cat.edis.tmiweb.services.fps.FPSServiceImpl">
        <constructor-arg type="java.lang.String" value="http://localhost:8888/someendpoint" />
        <constructor-arg type="java.lang.String" value="bill" />
        <constructor-arg type="java.lang.String" value="will" />
    </bean>

    <!--  Database stuff that we should separate out-->

    <bean id="fileDAO" class="cat.edis.tmiweb.dao.FileDAOImpl">
        <property name="sessionFactory">
            <ref bean="TMISessionFactory" />
        </property>
    </bean>

    <bean id="TMIDataSource" class="cat.cis.template.spring.util.TUFDataSource" destroy-method="close">
        <property name="poolName" value="db2OS390GenData" />
    </bean>

    <bean id="TMISessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="TMIDataSource" />

        <property name="mappingResources">
            <list>
                <!-- PUT HIBERNATE MAPPING FILES HERE -->
                <value>cat/edis/tmiweb/business/File.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <map>
                <entry>
                    <key>
                        <value>hibernate.show_sql</value>
                    </key>
                    <value>true</value>
                </entry>
                <entry>
                    <key>
                        <value>hibernate.default_schema</value>
                    </key>
                    <value>N4GK001$</value>
                </entry>
                <entry>
                    <key>
                        <value>hibernate.dialect</value>
                    </key>
                    <ref bean="TMIDialect" />
                </entry>
            </map>
        </property>
    </bean>

    <bean id="TMIDialect" factory-bean="TMIDialectFactory" factory-method="instance" />

    <bean id="TMIDialectFactory" class="cat.cis.template.spring.util.EnvironmentBeanStrategyFactory">
        <property name="envKey" value="tuf.environment" />
        <property name="defaultKey" value="DEV" />
        <property name="target">
            <map>
                <entry key="DEV" value="org.hibernate.dialect.DB2390Dialect" />
                <entry key="TEST" value="org.hibernate.dialect.DB2390Dialect" />
                <entry key="QA" value="org.hibernate.dialect.DB2390Dialect" />
                <entry key="PROD" value="org.hibernate.dialect.DB2390Dialect" />
            </map>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="TMISessionFactory" />
        </property>
    </bean>

</beans>
我的测试:

public void testSaveBlob() {
        FileDAO fileDAO =(FileDAO)SpringTestInitializer.getContext().getBean("fileDAO");
        File file = new File();
        file.setName("TestMe");
        file.setType("txt");
        file.setCreationTimeStamp(new Date());
        file.setDescription("idc");

        byte[] testBytes = new byte[1024];
        byte byteValue = 1;
        for (int i = 0; i < testBytes.length; i++) {
            testBytes[i] = byteValue;
        }
        file.setLength(new Long(testBytes.length));
        file.setContent(testBytes);
        fileDAO.save(file);

        File dbFile = fileDAO.load(file.getId());
        assertNotNull(file);
    }
跟踪:

org.springframework.dao.DataIntegrityViolationException: 无法更新: [cat.edis.tmiweb.business.File#0]; 嵌套异常是 org.hibernate.exception.DataException: 无法更新: [cat.edis.tmiweb.business.File#0]位于 org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:639) 在 org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412) 在 org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424) 在 org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) 在 org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:694) 在 cat.edis.tmiweb.dao.FileDAOImpl.save(FileDAOImpl.java:31) 在 cat.edis.tmiweb.dao.FileDAOImplTest.testSaveBlob(FileDAOImplTest.java:50) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(本机 方法)在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:85) 在 invoke(NativeMethodAccessorImpl.java:58) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:60) 在 java.lang.reflect.Method.invoke(Method.java:391) 在 runTest(TestCase.java:154) 在 org.jmock.core.VerifyingTestCase.runBare(VerifyingTestCase.java:39) 在 TestResult$1.protect(TestResult.java:106) 在 junit.framework.TestResult.runProtected(TestResult.java:124) 在 run(TestResult.java:109) 在 run(TestCase.java:118) 在 runTest(TestSuite.java:208) 在 run(TestSuite.java:203) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:436) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:311) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 原因: org.hibernate.exception.DataException: 无法更新: [cat.edis.tmiweb.business.File#0]位于 org.hibernate.exception.sqlstatecoverter.convert(sqlstatecoverter.java:77) 在 org.hibernate.exception.jdbceptionhelper.convert(jdbceptionhelper.java:43) 在 org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2430) 在 org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2312) 在 org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2612) 在 org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:96) 在 org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279) 在 org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263) 在 org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168) 在 org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298) 在 org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) 在 org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) 在 org.springframework.orm.hibernate3.HibernateAccessor.fluShifEssential(HibernateAccessor.java:390) 在 org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:420) ... 又有20人因以下原因而受伤: COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI驱动程序][DB2]SQL0100W否 找到用于获取、更新或删除的行 删除;或者查询的结果是 空桌子。SQLSTATE=02000

在 COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw\u SQLException(未知 来源)在 COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw\u SQLException(未知 来源)在 COM.ibm.db2.jdbc.app.SQLExceptionGenerator.check\返回\代码(未知 来源)在 COM.ibm.db2.jdbc.app.DB2PreparedStatement.loadParameters(未知 来源)在 COM.ibm.db2.jdbc.app.DB2PreparedStatement.execute2(未知 来源)在 COM.ibm.db2.jdbc.app.DB2PreparedStatement.executeUpdate(未知 来源)在 cat.cis.tuf.server.connector.jdbc.v1.JDBCv1DBStatement.executeUpdate(JDBCv1DBStatement.java:206) 在 org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23) 在 org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2408) ... 还有31个

异常对象:

 ex= DataException  (id=341)
backtrace= Object[35]  (id=349)
cause (NestableRuntimeException)= DB2Exception  (id=325)
cause (Throwable)= DataException  (id=341)
delegate= NestableDelegate  (id=350)
detailMessage= "could not update: [cat.edis.tmiweb.business.File#0]"
sql= "update N4GK001$.FILE set crte_ts=?, file_nm=?, file_typ_desc=?, file_desc=?, file_lgth=?, file_cntnt=? where gen_file_id=?"
sqle= DB2Exception  (id=325)
stackTrace= null
**

切换到类型4驱动程序:(仍然使用org.hibernate.dialogue.db2390dialogue)

新的错误**\

org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
   at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:672)
   at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
   at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
   at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
   at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:694)
   at cat.edis.tmiweb.dao.FileDAOImpl.save(FileDAOImpl.java:31)
   at cat.edis.tmiweb.dao.FileDAOImplTest.testSaveBlob(FileDAOImplTest.java:50)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:85)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:58)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:60)
   at java.lang.reflect.Method.invoke(Method.java:391)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at org.jmock.core.VerifyingTestCase.runBare(VerifyingTestCase.java:39)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:436)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:311)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
   at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
   at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
   at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:24)
   at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2408)
   at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2312)
   at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2612)
   at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:96)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
   at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
   at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:420)
   ... 20 more
删除/注释掉

File dbFile = fileDAO.load(file.getId()); assertNotNull(file); File dbFile=fileDAO.load(File.getId()); assertNotNull(文件); 然后从头开始重新运行测试

删除/注释掉

File dbFile = fileDAO.load(file.getId()); assertNotNull(file); File dbFile=fileDAO.load(File.getId()); assertNotNull(文件);
然后从头开始重新运行测试

从日志中可以看出,hibernate似乎插入了一个对象,然后尝试对其进行更新,但找不到刚刚插入的对象。我不确定,但这可能是个问题
org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
   at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:672)
   at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
   at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:424)
   at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
   at org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:694)
   at cat.edis.tmiweb.dao.FileDAOImpl.save(FileDAOImpl.java:31)
   at cat.edis.tmiweb.dao.FileDAOImplTest.testSaveBlob(FileDAOImplTest.java:50)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:85)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:58)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:60)
   at java.lang.reflect.Method.invoke(Method.java:391)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at org.jmock.core.VerifyingTestCase.runBare(VerifyingTestCase.java:39)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:436)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:311)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
   at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
   at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
   at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:24)
   at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2408)
   at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2312)
   at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2612)
   at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:96)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
   at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
   at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:420)
   ... 20 more
File dbFile = fileDAO.load(file.getId()); assertNotNull(file);