gpt4 book ai didi

java - HIbernate @OneToOne 映射

转载 作者:行者123 更新时间:2023-12-01 14:40:11 24 4
gpt4 key购买 nike

我有一个 PatientVisit.java,它与 PatientVisitObject.java 具有一对一的映射:

@Entity
@Table(name = "P_Visit")
public class PatientVisit extends Bean {
@Id
@Column(name = "PATIENT_VISIT_SEQ")
private Long patientVisitSeq;
@Column(name = "PATIENT_FIRST_NM")
private String firstName;
@Column(name = "PATIENT_LAST_NM")
private String lastName;
@Column(name = "PATIENT_MIDDLE_NM")
private String middleName;
@OneToOne
private PatientVisitObject pvo;
}

PatientVisitObject.java 有一个复合键。我需要将 key. PatientVisitSeq 映射到 PatientVisit.java 中的 PatientVisitSeq。

@Entity
@Table(name = "Patient_V_O")
public class PatientVisitObject extends Bean {
@Id
private PatientVisitObjectKey key;
@Column(name = "FIELD")
private String field;
}

关键是:

@Embeddable
public class PatientVisitObjectKey implements Serializable {
@Column(name = "PATIENT_VISIT_SEQ")
private Long patientVisitSeq;
@Column(name = "PATIENT_VISIT_OBJECT_SEQ")
private Long patientVisitObjectSeq;
}

我尝试过使用 @JoinTable 注释,但无法正确使用。有人可以给我一些指导吗?谢谢。

最佳答案

您需要使用双向映射,PatientVisit 是关系的反面:

public class PatientVisit extends Bean {
...
@OneToOne(mappedBy = "pv")
private PatientVisitObject pvo;
...
}

public class PatientVisitObject extends Bean {
@EmbeddedId
private PatientVisitObjectKey key;

@OneToOne
@MapsId("patientVisitSeq")
private PatientVisit pv;

...
}

另请参阅:

关于java - HIbernate @OneToOne 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16045228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com