我認爲(希望)我正在做所有正確的事情。 Google搜索了很多,無法找到問題所在。可以有多個Appointment
s。每個Appointment
有一個Department
。傳遞持久性&「分離的實體傳遞到堅持」
- 部門被加載和存儲在一個集合,當然,分離。
- 約會已創建並從上述集合中獲取部門。
- 約會被添加到配置文件。
- 個人資料持續存在。通過堅持
- 脫離實體拋出部
簡介:
@Entity
@Table(name = "PROFILE")
public class Profile {
/**
*
*/
private Set<Appointment> appointments = new HashSet<Appointment>();
/**
* @return the appointments
*/
@OneToMany(mappedBy = "profile", cascade = { CascadeType.PERSIST,
CascadeType.MERGE })
public Set<Appointment> getAppointments() {
return appointments;
}
/**
* @param appointments
* the appointments to set
*/
public void setAppointments(Set<Appointment> appointments) {
this.appointments = appointments;
}
/**
*
* @param apt
*/
public void addAppointment(Appointment apt) {
apt.setProfile(this);
appointments.add(apt);
}
}
約會:
@Entity
@Table(name = "APPOINTMENT")
public class Appointment {
/**
*
*/
private Department department;
private Profile profile;
/**
* @return the department
*/
@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)
@JoinColumn(name = "DEPARTMENT_ID")
@Index(name = "IDX_APPOINTMENT_DEPARTMENT")
@ForeignKey(name = "FK_APPOINTMENT_DEPARTMENT")
public Department getDepartment() {
return department;
}
/**
* @param department
* the department to set
*/
public void setDepartment(Department department) {
this.department = department;
}
/**
* @return the profile
*/
@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)
@JoinColumn(name = "PROFILE_ID")
@Index(name = "IDX_APPOINTMENT_PROFILE")
@ForeignKey(name = "FK_APPOINTMENT_PROFILE")
public Profile getProfile() {
return profile;
}
/**
* @param profile
* the profile to set
*/
public void setProfile(Profile profile) {
this.profile = profile;
}
}
部:
@Entity
@Table(name = "DEPARTMENT")
public class Department {
/**
*
*/
private Set<Appointment> appointments = new HashSet<Appointment>();
private Set<Department> children = new HashSet<Department>();
private Department parent;
/**
* @return the appointments
*/
@OneToMany(mappedBy = "department")
public Set<Appointment> getAppointments() {
return appointments;
}
/**
* @param appointments
* the appointments to set
*/
public void setAppointments(Set<Appointment> appointments) {
this.appointments = appointments;
}
/**
* @return the children
*/
@OneToMany(mappedBy = "parent", cascade = { CascadeType.PERSIST,
CascadeType.MERGE })
public Set<Department> getChildren() {
return children;
}
/**
* @param children
* the children to set
*/
public void setChildren(Set<Department> children) {
this.children = children;
}
/**
* @return the parent
*/
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PARENTDEPARTMENT_ID")
@Index(name = "IDX_DEPARTMENT_CHILDREN")
@ForeignKey(name = "FK_DEPARTMENT_CHILDREN")
public Department getParent() {
return parent;
}
/**
* @param parent
* the parent to set
*/
public void setParent(Department parent) {
this.parent = parent;
}
}
ŧ他犯錯誤的@Service
代碼:
@Transactional(propagation = Propagation.REQUIRED)
private Profile addNewProfile(ProfileJxb profileJxb) {
logger.entry();
Profile profile = new Profile(profileJxb);
for (AppointmentJxb aptJxb : profileJxb.getAppointments()
.getAppointmentJxb()) {
Appointment apt = new Appointment(aptJxb);
Department d = getDepartmentByName(aptJxb.getDepartment()); // previously fetched
apt.setDepartment(d);
// d.getAppointments().add(apt); // another failed attempt, this results in LazyInitException
profile.addAppointment(apt);
}
profile = profileDao.makePersistent(profile); // Exception thrown here!
logger.exit();
return profile;
}
我希望我失去了一些東西,因爲我取,並在持續的狀態設置Department
避免這種例外。
謝謝。
請檢查這個答案類似的問題http://stackoverflow.com/questions/13370221/jpa-hibernate-detached-entity-passed-to-persist/13500921#13500921 – Sam