gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:15:52 24 4
gpt4 key购买 nike

如果我从父表中删除一条记录,我希望子表中的相应记录也被删除。我怎样才能让 Hibernate 从 Child 表中删除而不是尝试用 null 更新?

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

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

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$$6a21cd58.delete(<generated>)
at com.dressbarn.imbo.model.data.hibernate.dao.CharityTransferDAOTest.testDelete(CharityTransferDAOTest.java:112)
at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)

我的表是:

家长:

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

<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);
}
}

最佳答案

我一直遇到这个错误。

只需在关系上加上一个 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>

基本上,相反的操作会告诉 hibernate child 不能在没有 parent 的情况下存在,从而导致 hibernate 删除 child 。

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

关于java - Hibernate 单向父/子关系 - delete() 对子表执行更新而不是删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1012874/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com