我有一个从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
的例外情况。任何人都可以帮我吗?
答案 0 :(得分:0)
尝试在@ManyToOne注释中添加'orphanRemoval = true'