How can get Entities which have collection of entities by ids entities in collection

#1
@Entity
@Table(name = "trains")
public class TrainEntity extends BaseEntity {

    @Column(name = "number", nullable = false)
    private Integer number;

    @ManyToMany
    @JoinTable(name = "train_stations", joinColumns = @JoinColumn(name = "train_id"),
            inverseJoinColumns = @JoinColumn(name = "station_id"))
    @OrderColumn(name = "id", nullable = false)
    private List<StationEntity> stationEntities;

    @Column(name = "seats", nullable = false)
    private Integer seats;

    @OneToMany(mappedBy = "trainEntity")
    private Set<TicketEntity> ticketEntities;

@Entity
@Table(name = "stations")
public class StationEntity extends BaseEntity{

    @Column(name = "station_name", nullable = false)
    private String name;

    @ManyToMany(mappedBy = "stationEntities")
    private Set<TrainEntity> trainEntities;

    @OneToMany(mappedBy = "firstStationEntity")
    private Set<WayEntity> waysFromThisStation;

    @OneToMany(mappedBy = "secondStationEntity")
    private Set<WayEntity> waysToThisStation;


public List<TrainEntity> getTrainsByStartAndDestStations(Long startStationId, Long destStationId) {
        Session session = getSession();
        List<TrainEntity> trains = session.createQuery("select t from TrainEntity t JOIN " +
                "t.stationEntities s where s.id =:startStation and s.id =:destStation").
                setParameter("startStation", startStationId).
                setParameter("destStation", destStationId).getResultList();
        return trains;
    }
0 Likes