JPA级联删除
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误的重要部分在下面以粗体显示:
EntityManager#remove(Object)
操作。正如 java 所记录的,它会抛出:因此,您实际上需要在删除实体之前重新附加实体:
或者,更简单地说:
The important part of the error is shown in bold below:
This is simply not allowed by the
EntityManager#remove(Object)
operation. As java documented, it throws:So you actually need to reattach the entity before to remove it:
Or, more simply: