org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted o

Exception in thread "main" org.hibernate.ObjectDeletedException:deleted object would be re-saved by cascade (remove deleted object from associations): [org.model.Login#30]
at org.hibernate.internal.SessionImpl.forceFlush(SessionImpl.java:1226)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:182)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:114)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:728)
at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:720)
at org.hibernate.engine.spi.CascadingAction$5.cascade(CascadingAction.java:258)
at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:380)
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:323)
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:208)
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:165)
at org.hibernate.event.internal.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:160)
at org.hibernate.event.internal.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:151)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:88)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
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 test.Test.main(Test.java:48)

删除的对象将会重新被保存起来....by cascade(都是级联惹得祸)

这里我的错误原因如下:

我的两个模型login表和detail表共用一个主键

login.cfg.xml的主键生成机制

<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="foreign">这个是受
<param name="property">detail</param>
</generator>
</id>

<one-to-one name="detail" class="org.model.Detail"constrained="true"></one-to-one> 一对一, detail表强势约束...

detail.cfg.xml的主键生成机制

  <id name="id" type="java.lang.Integer">
      <column name="ID" />
             <generator class="identity" />这个是攻
             </id>

<one-to-one name="login" class="org.model.Login" cascade="all" lazy="false"></one-to-one> 一对一 login表被级联增删改...

所以问题到这里...是的,login表一直是个受,根本不当家,我这时去login表里删东西就等于白费功夫,因为detail表会源源不断的给login表注入能量

擒贼先擒王!我应该直捣黄龙,直接去干掉detail表中的数据

错误语句:String hql="from Login where username='马赛克'";

正确语句:String hql="from Detail where username='马赛克'";

你可能感兴趣的:(级联增删改)