删除的对象将通过级联重新保存

时间:2013-11-08 06:33:26

标签: java hibernate

我有一个从DB中删除和保存对象的方法。

List<IpAddress> ipAddresses = ipAddressDAO.findByEmail("abc@gmail.com");
    if (employeeBean.getIpAddress() != null) {
        for (IpAddress ipAddress : ipAddresses) {
            HibernateUtil.getCurrentSession().delete(ipAddress );
        }
        String[] ipAddresses = employeeBean.getIpAddress().split(",");
        if (ipAddresses != null && ipAddresses.length > 0) {
            for (String ip : ipAddresses) {
                IpAddress ipAddress = new IpAddress();
                ipAddress.setEmailLogin(emailLogin);
                ipAddress.setIpAddress(ip);
                getSession().save(ipAddress);
            }
        }
    }

IpAddress.java

@Entity
public class IpAddress {
    private String ipAddress;
    @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
    @JoinColumn(name = "EmailLoginId")
    private EmailLogin emailLogin;

    public String getIpAddress() {
        return ipAddress;
    }

    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }

    public EmailLogin getEmailLogin() {
        return emailLogin;
    }

    public void setEmailLogin(EmailLogin emailLogin) {
        this.emailLogin = emailLogin;
    }
}

getSession().save(ipAddress);点,我得到了deleted object would be re-saved by cascade的例外情况。任何人都可以帮我吗?

1 个答案:

答案 0 :(得分:0)

尝试在@ManyToOne注释中添加'orphanRemoval = true'