1

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

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

IP地址.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。谁可以帮我这个事?

4

1 回答 1

0

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

于 2013-11-08T06:56:20.297 回答