我有下表:
@Entity(name = 'STUDENT')
class Student {
@Id
@Column(name = 'STUDENT_ID')
String studentId
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "id.student")
Set<Disabilities> disabilities = []
}
@Entity(name = 'STUDENT_DISABILITY')
class Disability {
@EmbeddedId
DisabilityId id
@Nullable
@Column(name = 'MOD_DT')
LocalDateTime modifiedDate
}
@Embeddable
class DisabilityId implements Serializable {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = 'STUDENT_ID')
Student student
@Column(name = 'DISABILITY_CD')
String disabilityCode
}
这一切都很好,但是我正在尝试将新表'Disability_info'表联接到'Disability'表。这是Disability_info表:
@Entity
@Table(name = 'DISABILITY_INFO')
class DisabilityInfo {
@Id
@Column(name = 'DISABILITY_CD')
String id
@Column(name = 'DISABILITY_NAME')
String disabilityName
@Column(name = 'DISABILITY_DESC')
String disabilityDesc
}
我遇到的问题是DisabilityInfo的主键是Disability类的复合键的一部分。我想要的就是下面的sql:
SELECT * FROM DISABILITY a INNER JOIN DISABILITY_INFO b on a.DISABILITY_CD = b.DISABILITY_CD
任何人都可以阐明我如何实现这一目标吗?
谢谢。
答案 0 :(得分:0)
我知道了。需要多对一的关联。我最初的想法是一个残疾应该有一个描述,因此存在1:1的关系。
但是,实际上,有多个残疾引用相同的描述,这意味着这实际上是多对一的关系!
@Entity(name = 'STUDENT_DISABILITY')
class Disability {
@EmbeddedId
DisabilityId id
@Nullable
@Column(name = 'MOD_DT')
LocalDateTime modifiedDate
@ManyToOne
@JoinColumn(name = 'DISABILITY_ID, insertable = false, updatable = false)
DisabilityInfo disabilityInfo
}