我对休眠的奇怪行为有疑问。
我有这样的代码:
Prepod prepod = new Prepod();
prepod.setId(1l);
Student student = new Student();
student.setAge(12l);
student.setName("name");
student.setId(1l);
prepod.getStudents().add(student);
student.getPrepods().add(prepod);
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.saveOrUpdate(prepod);
session.saveOrUpdate(student);
session.getTransaction().commit();
冬眠告诉我:
Hibernate: update Student set age=?, name=? where id=?
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:57)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3016)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2918)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3247)
at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:140)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:276)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1214)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:403)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
at logic.Main.main(Main.java:20)
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:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
但如果我使用
session.save(prepod);
session.save(student);
我懂了:
Hibernate: select max(id) from prepod
Hibernate: select max(id) from Student
Hibernate: insert into prepod (id) values (?)
Hibernate: insert into Student (age, name, id) values (?, ?, ?)
Hibernate: insert into prepod_Student (prepods_id, students_id) values (?, ?)
这对我来说是很好的行为。
你能解释一下为什么会这样吗?
之前我认为 saveOrUpdate 会自动选择保存或更新。