明确删除JPA关系 [英] Explicit delete on JPA relationships

查看:118
本文介绍了明确删除JPA关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对在JPA中管理关系有些困惑. 基本上我有两个具有一对多关系的实体

I am a bit confused about managing relationship in JPA. basically I have two entities with a One to Many relationship

配置可以具有一个或多个与之关联的电子邮件列表.

A configuration can have have a one or many email list associated with it.

@Entity
public class Config {
    @OneToMany(mappedBy="owner",cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    private List<Email> emailReceivers;
}
@Entity
public class Email {
    @ManyToOne
    private Config owner;
}

在EJB中,在更新/合并操作期间,我将编辑与配置关联的电子邮件列表, 我以为我不需要显式调用电子邮件实体上的delete操作,而只需通过删除配置电子邮件列表中的电子邮件来管理关系.

In an EJB and during update/merge operation wherein I would edit the list of emails associated with a configuration, I thought that I dont need to explicitly call the delete operation on my email Entity and I would just manage the relationship by deleting the email in my configuration email list.

@Stateless
public class ConfigFacadeImpl implements ConfigFacade{
    @EJB
    private ConfigDao configDao;
    @EJB
    private EmailDao emailDao;

    @Override
    public void update(Config Config, List<Email> emailsForDelete) {
        if(emailsForDelete!=null && emailsForDelete.size() > 0){
            for(Email emailTemp: emailsForDelete){
                Email email = emailDao.find(emailTemp.getId());
                emailDao.delete(email);  // Do I need to explicitly call the remove??
                config.getEmailReceivers().remove(email);
            }
        }
        configDao.update(config);
    }
}

如果我不执行删除操作,而只是将其从列表中删除,它将不会删除我的表行.

If I don't execute the delete and only remove it from the list, it wont erase my table row.

用户界面和数据库现在不同步,因为用户界面不会显示我已删除的电子邮件,但是当您检查数据库时,行仍然存在.

The UI and the database is now not in sync as the UI would not show the email(s) that I have deleted but when you check the database, the row(s) are still there.

是必需的吗?我以为JPA会为我处理这个问题,只要我将其删除即可.

Is it required? I thought JPA would handle this for me if I would just remove it in my entities.

更新

在进行任何更改之前,我已经对代码进行了调整,以便首先从数据库中获取实体,但是仍然没有删除我的子电子邮件实体.我想知道这是否是阿帕奇德比问题. (这是正确的方法,因为我正在将实体从JSF托管bean传递到EJB中,所以我需要先从数据库获取同步.)

I have tweaked my code to get the entity from the database first before making any changes but still it is not deleting my child email entities. I wonder if this is an apache derby issues. (This is the correct way right as I am passing my entities from my JSF managed bean into my EJB so I need to get the sync from the DB first.)

@Override
public void update(Config config, List<Email> emailsForDelete) {
    Config configTemp = configDao.find(config.getId());
    if(emailsForDelete!=null && emailsForDelete.size() > 0){
        for(Email emailTemp: emailsForDelete){
            configTemp.getEmailReceivers().remove(emailTemp);
        }
    }
    configDao.update(config);
}

推荐答案

由于您已经定义了层叠类型= CascadeType.ALL,因此JPA应该注意删除. Explicit Delete语句不是必需的.

Since you have already defined cascade type = CascadeType.ALL, JPA should take care of the deletion. Explicit Delete statement is not required.

这两个语句不是必需的:

These two statements are not required:

     Email email = emailDao.find(emailTemp.getId());
     emailDao.delete(email);  // Do I need to explicitly call the remove??

相反,您可能只想在config.getEmailReceivers()中找到匹配的emailReceiver并在执行操作时删除匹配的EmailReceivers.无需从数据库中加载Email实体.

Instead, you may want to just find the matching emailReceiver in config.getEmailReceivers() and remove the matching EmailReceivers as you are doing. There is no need to load the Email entity from the database.

要删除孤立对象,您可能希望将CascadeType.DELETE_ORPHAN级联属性与CascadeType.ALL一起包含.

To delete orphan objects, you may want to include CascadeType.DELETE_ORPHAN cascade attribute along with CascadeType.ALL.

这篇关于明确删除JPA关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆