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

插入数据的问题

Grade grade = (Grade)session.get(Students.class, 1);

    grade.setgid(3);

session.save(grade);

Transaction transaction = session.beginTransaction();

transaction.commit();

会报错为什么

正在回答

5 回答

回答错了,是因为在一个事务中更新了主键,而主键是不能被更新的.

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

我是重新插入一条数据也不行吗?

Students student1 = (Students)session.get(Students.class, 1);

    Students student2 = new Students();

    student2 = student1;

    student2.setSid(4);

Transaction transaction = session.beginTransaction();

session.save(student2);

transaction.commit();

session.close();

本来以为是获取的对象原因,后来声明了一个新的对象再修改其主键也报了相同的错误?

这是为什么?求解。。谢谢


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

因为你这里的gid还作为外键关联到student,如果这里只是grade一张表,没有跟其他表关联的话是没有问题的, 你把这个章节看完,后面有讲到关联关系的控制方向

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

org.hibernate.HibernateException: identifier of an instance of entity.Grade was altered from 1 to 3

at org.hibernate.event.internal.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:82)

at org.hibernate.event.internal.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:194)

at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:156)

at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:228)

at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:100)

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

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.imooc.test.test.updatetest(test.java:53)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:606)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)

at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)

at org.junit.runners.ParentRunner.run(ParentRunner.java:300)

at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)


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

你应该贴错误代码,贴这个又反映不出是哪里出错

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

zz2403558161 提问者

org.hibernate.HibernateException: identifier of an instance of entity.Grade was altered from 1 to 3 at org.hibernate.event.internal.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:82) at 跪求谢谢!!!!
2017-01-13 回复 有任何疑惑可以回复我~

举报

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

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

进入课程

插入数据的问题

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