JPA级联删除

发布于 2024-08-31 10:01:20 字数 1039 浏览 1 评论 0原文

我是 JPA/Hibernate 新手。目前使用EJB3、Hibernate/JPA。我有一个继承结构,如下所示。


@Entity
@DiscriminatorColumn(name = "form_type")
@Inheritance(strategy = InheritanceType.JOINED)
@GenericGenerator(name = "FORMS_SEQ", strategy = "sequence-identity", parameters = @Parameter(name = "sequence", value = "FORMS_SEQ"))
@Table(name = "Forms")
public abstract class Form{
    //code for Form
}

@Entity
@Table(name = "CREDIT_CARDS")
@PrimaryKeyJoinColumn(name="CREDIT_CARD_ID")
public class CreditCardForm extends Form {
       //Code for CreditCards.
}

当我添加带有保存的行时,行会正确插入到父表和子表中。但是,当我尝试删除时出现错误 -
10:19:35,465 错误 [TxPolicy] javax.ejb.EJBTransactionRolledbackException: 正在删除分离的实例 com.data.entities.form.financial.CreditCard#159?

我正在使用一个简单的 for 循环来确定继承类型-信用卡或借记卡,然后调用entityManager.remove(entity)。我做错了什么?

删除代码..

for(Form content: contents){
  if(content.getType()==Type.CREDIT_CARD){
     creditCardService.delete((CreditCard)content);
  }

谢谢。

WM

I am new to JPA/Hibernate. Currently using EJB3, Hibernate/JPA. I have an inheritacnce structure as follows..


@Entity
@DiscriminatorColumn(name = "form_type")
@Inheritance(strategy = InheritanceType.JOINED)
@GenericGenerator(name = "FORMS_SEQ", strategy = "sequence-identity", parameters = @Parameter(name = "sequence", value = "FORMS_SEQ"))
@Table(name = "Forms")
public abstract class Form{
    //code for Form
}

@Entity
@Table(name = "CREDIT_CARDS")
@PrimaryKeyJoinColumn(name="CREDIT_CARD_ID")
public class CreditCardForm extends Form {
       //Code for CreditCards.
}

When I add a row with save the rows are properly inserted into the parent and the child table. However when I try to delete I get an error -
10:19:35,465 ERROR [TxPolicy] javax.ejb.EJBTransactionRolledbackException: Removing a detached instance com.data.entities.form.financial.CreditCard#159?

I am using a simple for loop to determine the inheritance type - CreditCard or DebitCard and then calling entityManager.remove(entity). What am I doing wrong?

Code for delete..

for(Form content: contents){
  if(content.getType()==Type.CREDIT_CARD){
     creditCardService.delete((CreditCard)content);
  }

Thanks.

WM

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心的憧憬 2024-09-07 10:01:20

错误的重要部分在下面以粗体显示:

javax.ejb.EJBTransactionRolledbackException: Removing a detached instance com.data.entities.form.financial.CreditCard#159?

EntityManager#remove(Object) 操作。正如 java 所记录的,它会抛出:

IllegalArgumentException - 如果不是实体或如果是独立实体


因此,您实际上需要在删除实体之前重新附加实体:

CreditCard mergedCreditCard = em.merge(creditCard); // reattach the creditCard
em.remove(mergedCreditCard);

或者,更简单地说:

em.remove(em.merge(creditCard));    

The important part of the error is shown in bold below:

javax.ejb.EJBTransactionRolledbackException: Removing a detached instance com.data.entities.form.financial.CreditCard#159?

This is simply not allowed by the EntityManager#remove(Object) operation. As java documented, it throws:

IllegalArgumentException - if not an entity or if a detached entity

So you actually need to reattach the entity before to remove it:

CreditCard mergedCreditCard = em.merge(creditCard); // reattach the creditCard
em.remove(mergedCreditCard);

Or, more simply:

em.remove(em.merge(creditCard));    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文