为了账号安全,请及时绑定邮箱和手机立即绑定

测试保存时,报错a different object with the same identifier value was already associated with the session

然后我自己百度,有人说用merge()方法就可以解决,然而我使用之后又有心得错误。

INFO: HHH000232: Schema update complete

Hibernate: 

    select

        max(GID) 

    from

        gread

Hibernate: 

    select

        students0_.STUID as STUID1_0_0_,

        students0_.STUNAME as STUNAME2_0_0_,

        students0_.STUADDR as STUADDR3_0_0_,

        students0_.STUPHONE as STUPHONE4_0_0_ 

    from

        STUDENTS students0_ 

    where

        students0_.STUID=?

Hibernate: 

    insert 

    into

        gread

        (GNAME, GDESC, GID) 

    values

        (?, ?, ?)

Hibernate: 

    insert 

    into

        STUDENTS

        (STUNAME, STUADDR, STUPHONE, STUID) 

    values

        (?, ?, ?, ?)

Hibernate: 

    update

        STUDENTS 

    set

        STUNAME=?,

        STUADDR=?,

        STUPHONE=? 

    where

        STUID=?

Exception in thread "main" 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:81)

at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:73)

at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:59)

at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3224)

at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:3126)

at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3456)

at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:140)

at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377)

at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:369)

at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:287)

at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:339)

at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)

at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1234)

at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404)

at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)

at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)

at com.py4g.test.Test.add(Test.java:33)

at com.py4g.test.Test.main(Test.java:12)


正在回答

2 回答

你看看是不是没在hbm.xml文件那里把主键设置成自动增长型

1 回复 有任何疑惑可以回复我~

你先cfg.xml那里看看<property name="hbm2ddl.auto">update</property>这句,可能是update和create没用清的锅,可以重新来先create吧数据加进去然后改成update来测试,或者一直用create,可能是这里出了问题

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Hibernate初探之一对多映射
  • 参与学习       42171    人
  • 解答问题       295    个

Hibernate中一对多关联映射配置,以及cascade和inverse属性作用

进入课程

测试保存时,报错a different object with the same identifier value was already associated with the session

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信