HIbernate @OneToOne映射

时间:2013-04-16 19:14:43

标签: java hibernate

我有一个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注释并且无法正确使用它。有人可以给我一些指示。谢谢。

1 个答案:

答案 0 :(得分:1)

您需要使用双向映射,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;

    ...
}

另见: