使用:
假设当前布局
Activity (id,etc) [id is primary key]
AcitivityMapper (activityID, targetYearID) [composite key]
Years (targetYearID, targetYearName) [id primary key, name is unique]
我想,在给出“年份名称”列表的情况下,找到可以映射到至少一个年份名称的活动
额外说明:使用上述布局,活动可以映射到多年
示例:
如果给出包含“year1”或“year2”的列表,我想获得活动1
如何使用sql查询执行此操作?
---- Hibernate note
我的对象如下:
@Entity @Table(name="Activity")
class Activity {
@Id @Column(name="id") private Integer id;
@ManyToMany @JoinTable(name="ActivityMapper",joinColumns={@JoinColumn(name="id")},inverseJoinColumns{@JoinColumn(name="targetYearID")}) private List<TargetYear> targetYears = new ArrayList<>();
}
@Entity @Table(name="Years")
class TargetYear {
@Id @Column(name="targetYearID") private Integer id;
@Column(name="targetYearName") private String name;
}
答案 0 :(得分:0)
您可以使用以下本机SQL查询。
从活动中选择一个a.id,a.etc一个关于活动的mapMapper am.a = am.activityID其中am.targetYearID IN(从年y选择targetYearID,其中y.targetYearName = yearName
答案 1 :(得分:0)
SELECT DISTINCT
a.id,
a.etc
FROM years y
JOIN activitymapper am
ON (y.targetyearid = am.targetyearid)
JOIN activity a
ON (a.id = am.activityid)
WHERE y.targetyearname IN ('Year1', 'Year2')