【发布时间】:2019-05-24 04:39:03
【问题描述】:
我们有 2 个实体 Student 和 StudentDetails。如何声明它们之间的@OneToOne 关系?
@Table(name = "STUDENTS")
@Data
@IdClass(value = Student.StudentId.class)
public class Student implements Serializable {
@Id
@Column(name = "PARTITIONKEY")
private Long partitionKey;
@Id
@Column(name = "STUDENTKEY")
private Long studentKey;
@Data
@NoArgsConstructor
public static class StudentId implements Serializable {
private Long partitionKey;
private Long studentKey;
}
}
@Table(name = "STUDENTDETAILS")
public class StudentDetails implements Serializable {
@Id
@Column(name = "PARTITIONKEY")
private Long partitionKey;
@Id
@Column(name = "STUDENTKEY")
private Long studentKey;
@Data
@NoArgsConstructor
public static class StudentDetailsId implements Serializable {
private Long partitionKey;
private Long studentKey;
}
}
- @OneToOne 关系应该延迟加载
- insertable = false 和 updatable=false。
- 主键由应用程序生成
- 使用的库、数据库有 Spring data JPA、H2 in memory 数据库、MySQL、Hibernate 5
【问题讨论】:
标签: java hibernate jpa spring-data-jpa one-to-one