借地方请教一个HIBERNATE的灵异事件
java吧
全部回复
仅看楼主
level 1
@ManyToOnepublic Department getDep() {return dep;}public void setDep(Department dep) {this.dep = dep;}——————————————————————————@OneToMany(mappedBy ="dep")public List
getEmps() {return emps;}public void setEmps(List
emps) {this.emps = emps;}@OneToMany(mappedBy ="parent",fetch=FetchType.EAGER )public Set
getChildren() {return children;}public void setChildren(Set
children) {this.children = children;}——————————————————————@OneToMany(mappedBy ="dep")public List
getEmps() {return emps;}public void setEmps(List
emps) {this.emps = emps;}————————————————————————以上是我去掉所有级联后运行成功的代码奇怪的事一旦我加上级联 不管我删除哪个总说我已经删除的对象被级联保存抛出异常比如说department.getEmp().remove(employee);session.delete(employee);transacation.submit();这样就会报错 我明明已经移除了容器里的employee对象如果我在delete之前update(department)并提交就会抛出试图删除脏数据异常 删除的对象不存在但是如果我update之后不删除employee数据库中就根本没删除employee这条记录只是容器里已经没有了请问这是什么原因难道真的只能全部手工完成级联吗
2008年03月22日 05点03分 1
level 1
发下我现在能运行的删除代码//s.refresh(emp); new DAOMaster().delete(emp, session);if(emp.getDep()!=null){Department dep = emp.getDep();dep.getEmps().remove(dep);emp.setDep(null); }
2008年03月22日 05点03分 2
level 1
没学过看不懂怕闪
2008年03月22日 05点03分 4
level 1
下面贴的是能运行的代码..也就是我去除级联后手工删除集合的代码因为已经删除了级联所以删除dep不会跟着删除emp和子部门我出的问题就在级联上 如果我设置了cascade=CascadeType.All如果我删除集合内的一个对象(即使已经将此实体类从集合中移除)就会抛出删除的类被级联保存异常而如果我把实体类从集合中移除 更新department就会出现脏数据异常不能删除移除态的类而如果我更新后就不管了那实体类是没了可是数据库里记录还在
2008年03月22日 09点03分 7
level 1
多说无益 我贴完整代码吧
2008年03月22日 09点03分 8
level 1
package shadowingfly.beans;import java.util.List;import java.util.Set;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.ManyToOne;import javax.persistence.OneToMany;import javax.persistence.Transient;import org.apache.commons.lang.builder.EqualsBuilder;import org.apache.commons.lang.builder.HashCodeBuilder;@Entitypublic class Department implements BusinessEntity {private Long id;private Department parent;//父部门 private Set
children; //通过Set将下级加入该实体 子部门 private List
emps;//所属员工 private String name;//部门名称private boolean isRoot;//是否为根部门private String description;// 备注 public String toString() { return this.getName(); }public Department(String name, boolean isRoot, String description) {super();this.name = name;this.isRoot = isRoot;this.description = description;}public Department() {}@OneToMany(mappedBy ="dep")public List
getEmps() {return emps;}public void setEmps(List
emps) {this.emps = emps;}@OneToMany(mappedBy ="parent",fetch=FetchType.EAGER )public Set
getChildren() {return children;}public void setChildren(Set
children) {this.children = children;}@Id@GeneratedValue(strategy = GenerationType.TABLE)public Long getId() {return id;}public void setId(Long id) {this.id = id;}@ManyToOnepublic Department getParent() {return parent;} public void setParent(Department parent){this.parent = parent;}public String getName() {return name;}public void setName(String name) {this.name = name;}public boolean isRoot() {return isRoot;}public void setRoot(boolean isRoot) {this.isRoot = isRoot;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}@Override public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof Department)) { return false; } Department dep = (Department) obj; if (isTransient()) { return equalsForTransient(dep); } return equalsForDetached(dep); } private boolean equalsForTransient(Department d) { return new EqualsBuilder() .append(getName(), d.getName()).append(getId(),d.getId()).isEquals(); } private boolean equalsForDetached(Department d) { return new EqualsBuilder() .append(getId(), d.getId()) .isEquals(); } @Override public int hashCode() { if (isTransient()) { return hashCodeForTransient(); } return hashCodeForDetached(); } private int hashCodeForTransient() { return new HashCodeBuilder(6120227, 2281823) .append(getName()) .toHashCode(); } private int hashCodeForDetached() { return new HashCodeBuilder(6120227, 2281823) .append(getId()) .toHashCode(); }@Transientpublic boolean isTransient() {if(getId()==null)return false;return getId() < 1;}}
2008年03月22日 09点03分 9
level 1
太长..要审核
2008年03月22日 09点03分 10
level 1
package shadowingfly.beans;import java.util.Date;import java.util.Map;import javax.persistence.CascadeType;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.ManyToMany;import javax.persistence.ManyToOne;import javax.persistence.OneToOne;import javax.persistence.Transient;import org.apache.commons.lang.builder.EqualsBuilder;import org.apache.commons.lang.builder.HashCodeBuilder;import org.hibernate.annotations.MapKeyManyToMany;@Entitypublic class Employee implements BusinessEntity {private Long id;private Department dep;// 部门private String name;// 姓名private Date birthday; // 生日private String gender;// 性别private String address;// 地址private String phone;// 电话private String state;// 状态 进厂/炒更/请假private int basicWage;// 基本工资private Date datecode;// 入厂日期private String description;// 备注private Map
parts; //此员工完成的工序private Othersub other;//杂项public Employee(String name, Date birthday, String gender, String address,String phone, String state, int basicWage, Date datecode,String description) {super();this.name = name;this.birthday = birthday;this.gender = gender;this.address = address;this.phone = phone;this.state = state;this.basicWage = basicWage;this.datecode = datecode;this.description = description;}public Employee() {super();} public String toString() { return this.getName(); }@Id@GeneratedValue(strategy = GenerationType.TABLE)public Long getId() {return id;}public void setId(Long id) {this.id = id;}@ManyToOnepublic Department getDep() {return dep;}public void setDep(Department dep) {this.dep = dep;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public String getGender() {return gender;}
2008年03月22日 09点03分 11
level 1
public void setGender(String gender) {this.gender = gender;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public String getState() {return state;}public void setState(String state) {this.state = state;}public int getBasicWage() {return basicWage;}public void setBasicWage(int basicWage) {this.basicWage = basicWage;}public Date getDatecode() {return datecode;}public void setDatecode(Date datecode) {this.datecode = datecode;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}@ManyToMany(fetch=FetchType.EAGER) @MapKeyManyToManypublic Map
getParts() {return parts;}public void setParts(Map
parts) {this.parts = parts;}@OneToOne(fetch=FetchType.EAGER,cascade=CascadeType.ALL)public Othersub getOther() {return other;}public void setOther(Othersub other) {this.other = other;}@Transientpublic boolean isTransient() {if(getId()==null)return false;return getId() < 1;}public boolean equalsForDetached(Object obj){ Employee e = (Employee)obj; return new EqualsBuilder().append(getId(), e.getId()).append(getName(),e.getName() ) .append(getBirthday(),e.getBirthday()).isEquals();}public boolean equalsForTransient(Object obj){ Employee e = (Employee)obj; return new EqualsBuilder().append(getName(),e.getName() ) .append(getBirthday(),e.getBirthday()).isEquals();}@Overridepublic boolean equals(Object obj){if(obj == this)return true;if(!(obj instanceof Employee))return false;if(isTransient()){return equalsForTransient(obj);}return equalsForDetached(obj);} @Override public int hashCode() { if (isTransient()) { return hashCodeForTransient(); } return hashCodeForDetached(); } private int hashCodeForTransient() { return new HashCodeBuilder(6120227, 2281823) .append(getName()) .append(getBirthday()) .toHashCode(); } private int hashCodeForDetached() { return new HashCodeBuilder(6120227, 2281823) .append(getId()) .toHashCode(); }}
2008年03月22日 09点03分 12
level 1
这是两个entity bean下面贴DELETER
2008年03月22日 09点03分 13
level 1
我改回级联关系就报这个错 org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations): [shadowingfly.beans.Department#98305]
2008年03月22日 10点03分 16
level 1
就报这个错 org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations):
2008年03月22日 11点03分 18
level 1
级联是单方面级联 即容器的持有者那边设置cascadeType=CascadeType.ALL
2008年03月22日 11点03分 19
level 2
你的映射文件中many-to-one怎么配的?Set
children这个set的级联怎么配的?
2008年03月22日 11点03分 20
level 0
我这个是annotation映射具体映射在这里@OneToMany(mappedBy ="parent",fetch=FetchType.EAGER,cascade=CascadeType.ALL ) public Set
getChildren() { return children; } @OneToMany(mappedBy ="dep",cascade=CascadeType.ALL) public List
getEmps() { return emps; }
2008年03月22日 11点03分 21
level 0
就这两处改了 其他跟楼上的一样双向级联我也试过 错误更严重
2008年03月22日 11点03分 22
level 1
package shadowingfly.beans;import java.util.Date;import java.util.Map;import javax.persistence.CascadeType;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.ManyToMany;import javax.persistence.ManyToOne;import javax.persistence.OneToOne;import javax.persistence.Transient;import org.apache.commons.lang.builder.EqualsBuilder;import org.apache.commons.lang.builder.HashCodeBuilder;import org.hibernate.annotations.MapKeyManyToMany;@Entitypublic class Employee implements BusinessEntity {private Long id;private Department dep;// 部门private String name;// 姓名private Date birthday; // 生日private String gender;// 性别private String address;// 地址private String phone;// 电话private String state;// 状态 进厂/炒更/请假private int basicWage;// 基本工资private Date datecode;// 入厂日期private String description;// 备注private Map
parts; //此员工完成的工序private Othersub other;//杂项public Employee(String name, Date birthday, String gender, String address,String phone, String state, int basicWage, Date datecode,String description) {super();this.name = name;this.birthday = birthday;this.gender = gender;this.address = address;this.phone = phone;this.state = state;this.basicWage = basicWage;this.datecode = datecode;this.description = description;}public Employee() {super();} public String toString() { return this.getName(); }@Id@GeneratedValue(strategy = GenerationType.TABLE)public Long getId() {return id;}public void setId(Long id) {this.id = id;}@ManyToOnepublic Department getDep() {return dep;}public void setDep(Department dep) {this.dep = dep;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public String getState() {return state;}public void setState(String state) {this.state = state;}public int getBasicWage() {return basicWage;}public void setBasicWage(int basicWage) {this.basicWage = basicWage;}public Date getDatecode() {return datecode;}public void setDatecode(Date datecode) {this.datecode = datecode;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}@ManyToMany(fetch=FetchType.EAGER) @MapKeyManyToManypublic Map
getParts() {return parts;}public void setParts(Map
parts) {this.parts = parts;}@OneToOne(fetch=FetchType.EAGER,cascade=CascadeType.ALL)public Othersub getOther() {return other;}public void setOther(Othersub other) {this.other = other;}@Transientpublic boolean isTransient() {if(getId()==null)return false;return getId() < 1;}public boolean equalsForDetached(Object obj){ Employee e = (Employee)obj; return new EqualsBuilder().append(getId(), e.getId()).append(getName(),e.getName() ) .append(getBirthday(),e.getBirthday()).isEquals();}public boolean equalsForTransient(Object obj){ Employee e = (Employee)obj; return new EqualsBuilder().append(getName(),e.getName() ) .append(getBirthday(),e.getBirthday()).isEquals();}@Overridepublic boolean equals(Object obj){if(obj == this)return true;if(!(obj instanceof Employee))return false;if(isTransient()){return equalsForTransient(obj);}return equalsForDetached(obj);} @Override public int hashCode() { if (isTransient()) { return hashCodeForTransient(); } return hashCodeForDetached(); } private int hashCodeForTransient() { return new HashCodeBuilder(6120227, 2281823) .append(getName()) .append(getBirthday()) .toHashCode(); } private int hashCodeForDetached() { return new HashCodeBuilder(6120227, 2281823) .append(getId()) .toHashCode(); }}
2008年03月22日 12点03分 23
level 0
orz...楼上是下午发的 现在才过审核..谁帮我删了
2008年03月22日 12点03分 24
level 1
不厚道的人工置顶一下...
2008年03月23日 04点03分 25
level 1
问题已解决...原因是hibernate不支持递归级联即我删除了一个部门 他级联就删除了子部门和员工 但是没法删除子部门的子部门和员工 故报错
2008年03月23日 05点03分 26
1 2 尾页