在Hibernate中,它可能没有关于集合映射中的级联的问题。但我遇到过.l在Hibernate 4.2中使用Annotation。
这是我的实体类:
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.GenericGenerator;
import com.partysys.partymanage.deus.entity.Deus;
import com.partysys.partymanage.period.entity.Period;
import com.partysys.sysmanage.branch.entity.Branch;
@Entity
@Table(name = "partymember")
public class Partymember implements java.io.Serializable {
private String id;
private String password;
private String name;
List<String> cultivate;
public Partymember() {
}
public Partymember(String id) {
super();
this.id = id;
}
@GenericGenerator(name = "generator", strategy = "uuid.hex")
@Id
@GeneratedValue(generator = "generator")
@Column(name = "partymember_id", unique = true, nullable = false, length = 32)
public String getId() {
return this.id;
}
public void setId(String id) {
this.id = id;
}
@Column(name="password", length=25)
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Column(name = "name", length = 30, nullable=false)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@ElementCollection(targetClass=String.class, fetch=FetchType.EAGER)
@CollectionTable(name="cultivate_person",joinColumns=@JoinColumn(name="partymember_id",nullable=false))
@Column(name = "cultivate", length = 20)
@OrderColumn(name="t_order")
public List<String> getCultivate() {
return this.cultivate;
}
public void setCultivate(List<String> cultivate) {
this.cultivate = cultivate;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Partymember other = (Partymember) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
但是当我尝试删除聚会成员中的数据时,会出错。
BaseDaoImpl.java:
public void delete(Serializable id)
{
getSessionFactory().getCurrentSession()
.createQuery("delete " + clazz.getSimpleName()
+ " en where en.id = ?0")
.setParameter("0" , id)
.executeUpdate();
}
PartymemberAction.java:
partymemberService.delete(partymember.getId());
PartymemberServiceImpl.java:
@Override
public void delete(Serializable id) {
partymemberDao.deleteUserRoleByUserId(id);
partymemberDao.delete(id);
}
堆栈跟踪:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`partysys`.`cultivate_person`, CONSTRAINT `FK_sllcrigxee4qhp11422d59mop` FOREIGN KEY (`partymember_id`) REFERENCES `partymember` (`partymember_id`))
但是当我删除Partymember中的数据时,为什么它不会删除training(集合映射)中的数据?太奇怪了! 谢谢!
答案 0 :(得分:0)
您的子表有外键,您没有启用级联删除。
因此,当您尝试删除父行时,子行不会被删除,这就是您面临MySQLIntegrityConstraintViolationException的原因。
在删除父行时,应启用级联删除以删除所有相关子行。
语法如下所示
@OneToMany(mappedBy="parent_table", cascade={CascadeType.PERSIST, CascadeType.REMOVE})