java Hibernate 单向父/子关系 - delete() 对子表执行更新而不是删除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1012874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:48:10  来源:igfitidea点击:

Hibernate Unidirectional Parent/Child relationship - delete() performs update on child table instead of delete

javahibernatemappingone-to-many

提问by Mark Glass

If I delete a record from the Parent table I want the corresponding records in the child table to be deleted. How can I make Hibernate delete from the Child table rather than attempt to update with a null?

如果我从父表中删除一条记录,我希望删除子表中的相应记录。如何从子表中删除 Hibernate,而不是尝试使用 null 进行更新?

I'm using Hibernate 3 but cannot use annotations at this time. I've attached copies of HBM, DAO etc below. -- Thank you in Advance

我正在使用 Hibernate 3,但目前无法使用注释。我在下面附上了 HBM、DAO 等的副本。 - 先感谢您

When attempting to delete data from tables in Parent/Child relationship I get the following error:

尝试从父/子关系中的表中删除数据时,出现以下错误:

Testcase: testDelete(com.dressbarn.imbo.model.data.hibernate.dao.CharityTransferDAOTest):        Caused an ERROR
Hibernate flushing: Could not execute JDBC batch update; uncategorized SQLException for SQL [update RMS12.DRS_CHARITY_TRANSFER_ITEM set TSF_NO=null, TSF_SEQ_NO=null where TSF_NO=?]; SQL state [72000]; error code [1407]; ORA-01407: cannot update ("RMS12"."DRS_CHARITY_TRANSFER_ITEM"."TSF_NO") to NULL
; nested exception is java.sql.BatchUpdateException: ORA-01407: cannot update ("RMS12"."DRS_CHARITY_TRANSFER_ITEM"."TSF_NO") to NULL

org.springframework.jdbc.UncategorizedSQLException: Hibernate flushing: Could not execute JDBC batch update; uncategorized SQLException for SQL [update RMS12.DRS_CHARITY_TRANSFER_ITEM set TSF_NO=null, TSF_SEQ_NO=null where TSF_NO=?]; SQL state [72000]; error code [1407]; ORA-01407: cannot update ("RMS12"."DRS_CHARITY_TRANSFER_ITEM"."TSF_NO") to NULL
; nested exception is java.sql.BatchUpdateException: ORA-01407: cannot update ("RMS12"."DRS_CHARITY_TRANSFER_ITEM"."TSF_NO") to NULL

Caused by: java.sql.BatchUpdateException: ORA-01407: cannot update ("RMS12"."DRS_CHARITY_TRANSFER_ITEM"."TSF_NO") to NULL

        at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:498)
        at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:12368)
        at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
        at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
        at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
        at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
        at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
        at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
        at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:578)
        at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:662)
        at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:632)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:314)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:116)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
        at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:629)
        at com.dressbarn.imbo.model.data.hibernate.dao.CharityTransferDAO$$EnhancerByCGLIB$a21cd58.delete(<generated>)
        at com.dressbarn.imbo.model.data.hibernate.dao.CharityTransferDAOTest.testDelete(CharityTransferDAOTest.java:112)
        at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)

My tables are:

我的表是:

Parent:

家长

CREATE TABLE DRS_CHARITY_TRANSFER
(
  TSF_NO          NUMBER(10)                    NOT NULL Primary Key,
  FROM_LOC        NUMBER(10),
  CHARITY_LOC_ID  NUMBER(10),
  STATUS          VARCHAR2(1 CHAR),
  CREATE_DATE     DATE,
  EXT_REF_NO      VARCHAR2(30 CHAR),
  COMMENT_DESC    VARCHAR2(2000 CHAR),
  USER_ID         VARCHAR2(30 CHAR)
)

Child:

孩子:

CREATE TABLE DRS_CHARITY_TRANSFER_ITEM
 (
  TSF_NO      NUMBER(10)  NOT NULL PRIMARY KEY,
  ITEM        VARCHAR2(25 BYTE)  NOT NULL PRIMARY KEY,
  TSF_SEQ_NO  INTEGER,
  TSF_QTY     INTEGER
)

HBM XML

HBM XML

<hibernate-mapping package="com.dressbarn.imbo.model.data.hibernate.transfer" schema="RMS12">
   <class name="CharityTransfer" table="DRS_CHARITY_TRANSFER">
       <id name="transferNumber" column="TSF_NO" unsaved-value="undefined">

       </id>
       <property column="FROM_LOC" length="10" name="fromLocation" type="java.lang.Long"/>
       <property column="CHARITY_LOC_ID" length="10" name="toCharityLocId" type="java.lang.Long"/>
       <property column="STATUS" name="status" type="string"/>
       <property column="EXT_REF_NO" name="documentNumber" type="string"/>
       <property column="COMMENT_DESC" name="comment" type="string"/>
       <property column="CREATE_DATE" name="createDate" type="string"/>
       <property column="USER_ID" name="userId" type="string"/>
    <list name="charityTransferItemList" cascade="all-delete-orphan" lazy="false">
        <key column="TSF_NO" />
        <list-index column="TSF_SEQ_NO"/>
        <one-to-many class="CharityTransferItem" />
    </list>
</class>

<class name="CharityTransferItem" table="DRS_CHARITY_TRANSFER_ITEM">
    <id name="item" column="TSF_NO" unsaved-value="undefined">

    </id>
    <property column="ITEM" name="item" type="string"/>
    <property column="TSF_SEQ_NO" length="10" name="sequence" type="integer"/>
    <property column="TSF_QTY" length="12" name="quantity" type="long"/>
</class>

DAO

public class CharityTransferDAO extends HibernateDaoSupport implements ICharityTransfer {

   public void delete(CharityTransfer charityTransfer) throws IMADataException {
       try {
         getSessionFactory()
                 .getCurrentSession()
                 .delete(charityTransfer);
      }
      catch (HibernateException e) {
        throw new IMADataException("failed to delete charity shipping information", e);
      }    
}

回答by Michael Wiles

I encountered this error allthe time.

我遇到这个错误所有的时间。

Just put an inverse="true" on the relationship and your problem will go away!

只需在关系上加上 inverse="true" ,您的问题就会消失!

<list name="charityTransferItemList" inverse="true" cascade="all-delete-orphan" lazy="false" >
        <key column="TSF_NO" />
        <list-index column="TSF_SEQ_NO"/>
        <one-to-many class="CharityTransferItem" />
    </list>

Basically the inverse will tell hibernate the child cannot exist without a parent, thereby causing hibernate to delete the child.

基本上相反会告诉 hibernate 如果没有父级,孩子就不能存在,从而导致 hibernate 删除孩子。

Having said that, you'll also need to remove the charityTransfer object from the collection in the parent as well.

话虽如此,您还需要从父级的集合中删除charityTransfer 对象。

回答by jjmontes

(Note: This has been raised as an incorrect answer by two users).

(注意:这已被两个用户提出为错误答案)。

Hibernate docs say you can just mark the key column as not-null="true":

Hibernate 文档说您可以将键列标记为not-null="true"

<list name="charityTransferItemList" cascade="all,delete-orphan" lazy="false">
        <key column="TSF_NO" not-null="true" />
        <list-index column="TSF_SEQ_NO"/>
        <one-to-many class="CharityTransferItem" />
</list>

From Hibernate doc on collections:

来自关于集合的 Hibernate 文档

If the foreign key column of a association is declared NOT NULL, you must declare the mapping not-null="true" or use a bidirectional association with the collection mapping marked inverse="true". See the discussion of bidirectional associations later in this chapter for more information.

如果关联的外键列声明为 NOT NULL,则必须声明映射 not-null="true" 或使用带有标记为 inverse="true" 的集合映射的双向关联。有关更多信息,请参阅本章后面对双向关联的讨论。

I also think there is a typo in your cascade style (all-delete-orphanshould be all,delete-orphan).

我还认为您的级联样式中有一个错字(all-delete-orphan应该是all,delete-orphan)。