从两个表中获取多对多的记录

时间:2018-08-28 22:36:40

标签: java hibernate spring-data

我有两个表,多对多

public class Repertoire {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(nullable = false, unique = true)
    private Integer id;

    private String name;
    private Integer dayWeek;

    @ManyToMany(cascade = CascadeType.REMOVE)
    @JoinTable(
            name = "repertoire_seance",
            joinColumns = { @JoinColumn(name = "repertoire_id")},
            inverseJoinColumns = {@JoinColumn(name = "seance_id")}
    )
    List<Seance> seances = new ArrayList<>();
}

    public class Seance {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(nullable = false, unique = true)
    private Integer id;

    private java.time.LocalTime displayTime;

    @ManyToOne
    private Film film;

    @Column(length=127)
    private String kind;


    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
    private Hall hall;

    @OneToMany(mappedBy = "reservationSeance")
    @JsonIgnore
    private List<Reservation> reservations = new ArrayList<>();
}

使用弹簧数据(JpaRepository)之类的findBySeanceIdAndRepertoireDayWeek()之类的休眠创建链接表repertoire_seance是否可以通过seancesId和repertoire.dayWeek获得机会?

1 个答案:

答案 0 :(得分:0)

您可以通过编写HQL来实现。

它看起来像这样:

dataStamp