我对hibernate奇怪的行为有疑问。
我有这样的代码:
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说我:
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会自动选择保存或更新。
答案 0 :(得分:2)
问题在于区分对象是否存在于DB中。
对象Prepod
和Student
的ID显然设置为generator="increment"
(这就是我们看到Select Max(id)
语句的原因)。而且unsaved-value
很可能是默认值:0。
这意味着Hibernate会将Prepod实例视为新的/瞬态的,以防ID为== 0.但在您的代码中,您设置了id:student.setAge(12l);
因此,对象不再是瞬态的(从Hibernate的角度来看)。它似乎是现有的,已经存在的对象。
因此,如果调用SaveOrUpdate
,Hibernate会使用UPDATE
语句,因为该对象似乎不是瞬态的。
如何解决? 不将ID设置为自己。只需依靠generator="increment"
。在这种情况下,对象将是瞬态的(默认id == 0),Hibernate将按预期工作