说我有一个名为entity
的{{1}},如下所示
UserSchedule
在我的@Entity
@Table(name = "user_schedule")
public class UserSchedule {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "user_id")
private long userId;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "schedule_id", referencedColumnName = "id")
private Set<Schedule> schedules;
}
中,我想过滤日程安排。这样的事情UserScheduleRepository
。但由于时间表是一个集合而不是@Query("select us from UserSchedule us where us.schedules.id = 1")
实体,因此这不合法。如何在休眠中实现这一点?
答案 0 :(得分:1)
您可以使用JPQL联接(JPQL Joins Documentation)
@Query("select us from UserSchedule us, IN(us.schedules) sh where sh.id = ?1")
UserSchedule getByScheduleId1(long id);
@Query("select us from UserSchedule us join us.schedules sh where sh.id = ?1")
UserSchedule getByScheduleId2(long id);
如果您有@ManyToOne
反向引用字段,请
@Entity
@Table(name = "schedule")
public class Schedule {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@ManyToOne
private UserSchedule schedule;
...
您也可以尝试类似
的内容@Query("select us from UserSchedule us where us.id in (select s.schedule from Schedule s where s.id = ?1 )")