@OneToMany 双向关系 returns null 列表
@OneToMany bidirectional relation returns null List
我有以下内容:
@Entity
@Table(name = "patient_identifiers")
public class PatientIdentity extends AbstractEntity {
@ManyToOne
@JoinColumn(name = "patient_id")
private Patient patient;
和
@Entity
@Table(name = "patients")
public class Patient extends AbstractEntity {
@OneToMany(mappedBy = "patient", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<PatientIdentity> urs;
对象正确保留,但是如果我使用 Patient 对象检索列表,则列表为空。我猜是不是和这两种关系有冲突有关?
结构的原因是有时我们只知道患者的身份,因此我们可以从身份创建患者对象,但稍后我们需要使用患者对象来检索患者的详细信息(包括身份)。
在跨国公司中,我正在创建病理学、患者和身份对象,因为它们都是从一条消息中派生出来的。所以一个病人可以有很多身份和病症。然后我需要通过 AMQP 将病理学作为 JSON 转发,包括身份。这是它失败的地方:Pathology.getPatient.getPrimaryIdentity PrimaryIdentity 派生自身份列表。
myIdentity = new PatientIdentity();
myIdentity.setUr(ur);
myIdentity.setCreated(new Date());
myIdentity.setPrimary(true);
patientIdentityService.create(myIdentity);
Patient myPatient = new Patient();
myPatient.setDateOfBirth(dateOfBirth);
myPatient.setDateOfDeath(dateOfDeath);
myPatient.setLastUpdated(new Date());
repo.saveAndFlush(myPatient);
myIdentity.setPatient(myPatient);
我尝试了以下但只是以异常循环结束
myPatient.getUrs().add(myIdentity);
我正在使用 EclipseLink/Spring 数据
你需要在两端维护PatientIdentity和Patient的关系。最好的方法是在 Patient addIdentity(PatientIdentity patientIdentity);
中添加一个方法
public void addIdentity(PatientIdentity patientIdentity) {
if (urs=null) {
urs = new ArrayList<PatientIdentity>();
}
urs.add(patientIdentity);
patientIdentity.setPatient(this);
}
myIdentity = new PatientIdentity();
myIdentity.setUr(ur);
myIdentity.setCreated(new Date());
myIdentity.setPrimary(true);
// you don't need this code any longer since you are cascading your persist.
//patientIdentityService.create(myIdentity);
Patient myPatient = new Patient();
myPatient.setDateOfBirth(dateOfBirth);
myPatient.setDateOfDeath(dateOfDeath);
myPatient.setLastUpdated(new Date());
myPatient.addIdentity(myIdentity);
// this will cascade the persist
repo.saveAndFlush(myPatient);
我有以下内容:
@Entity
@Table(name = "patient_identifiers")
public class PatientIdentity extends AbstractEntity {
@ManyToOne
@JoinColumn(name = "patient_id")
private Patient patient;
和
@Entity
@Table(name = "patients")
public class Patient extends AbstractEntity {
@OneToMany(mappedBy = "patient", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<PatientIdentity> urs;
对象正确保留,但是如果我使用 Patient 对象检索列表,则列表为空。我猜是不是和这两种关系有冲突有关?
结构的原因是有时我们只知道患者的身份,因此我们可以从身份创建患者对象,但稍后我们需要使用患者对象来检索患者的详细信息(包括身份)。
在跨国公司中,我正在创建病理学、患者和身份对象,因为它们都是从一条消息中派生出来的。所以一个病人可以有很多身份和病症。然后我需要通过 AMQP 将病理学作为 JSON 转发,包括身份。这是它失败的地方:Pathology.getPatient.getPrimaryIdentity PrimaryIdentity 派生自身份列表。
myIdentity = new PatientIdentity();
myIdentity.setUr(ur);
myIdentity.setCreated(new Date());
myIdentity.setPrimary(true);
patientIdentityService.create(myIdentity);
Patient myPatient = new Patient();
myPatient.setDateOfBirth(dateOfBirth);
myPatient.setDateOfDeath(dateOfDeath);
myPatient.setLastUpdated(new Date());
repo.saveAndFlush(myPatient);
myIdentity.setPatient(myPatient);
我尝试了以下但只是以异常循环结束
myPatient.getUrs().add(myIdentity);
我正在使用 EclipseLink/Spring 数据
你需要在两端维护PatientIdentity和Patient的关系。最好的方法是在 Patient addIdentity(PatientIdentity patientIdentity);
中添加一个方法public void addIdentity(PatientIdentity patientIdentity) {
if (urs=null) {
urs = new ArrayList<PatientIdentity>();
}
urs.add(patientIdentity);
patientIdentity.setPatient(this);
}
myIdentity = new PatientIdentity();
myIdentity.setUr(ur);
myIdentity.setCreated(new Date());
myIdentity.setPrimary(true);
// you don't need this code any longer since you are cascading your persist.
//patientIdentityService.create(myIdentity);
Patient myPatient = new Patient();
myPatient.setDateOfBirth(dateOfBirth);
myPatient.setDateOfDeath(dateOfDeath);
myPatient.setLastUpdated(new Date());
myPatient.addIdentity(myIdentity);
// this will cascade the persist
repo.saveAndFlush(myPatient);