java - JpaRepository findBy 引用表中的字段

标签 java spring-boot spring-data-jpa jpql

@Entity
@Table(name="seance")
@Data
public class Seance {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(nullable = false, unique = true)
    private Integer id;

    private java.time.LocalTime displayTime;

    @ManyToMany(mappedBy = "seances")
    @JsonIgnore
    private List<Repertoire> repertoires = new ArrayList<>();

    @OneToMany(mappedBy = "reservationSeance", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JsonIgnore
    private List<Reservation> reservations = new ArrayList<>();

}


@Entity
@Table(name="reservation")
@Data
public class Reservation {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(nullable = false, unique = true)
    private Integer id;

    private java.time.LocalDate reservationDate;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "id_seance")
    private Seance reservationSeance;

   .........

}

我有两个与 ManyToOne 相关的实体,并且我尝试扩展 JpaRepository

public interface ReservationRepository extends JpaRepository<Reservation,Integer> {
    List<Reservation> findReservationsByReservationSeance(Seance seance);
}

但是作为参数,我必须使用所有对象 Seance,可以使用 JpaRepository 仅通过 Seance 表中的 id 和 displayTime 查找预订,还是需要编写自己的方法?

最佳答案

您可以在 ReservationRepository 中添加您自己的查询

@Query("SELECT r FROM Reservation r WHERE r.reservationSeance.id=:seanceId AND r.reservationSeance.displayTime = :displayTime")
public List<Reservation> findReservations(@Param("seanceId") int seanceId, @Param("displayTime") LocalTime displayTime);

关于java - JpaRepository findBy 引用表中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51245892/

相关文章:

database - Spring 数据 : CrudRepository's save method and update

java - "resource is out of sync with the filesystem"错误的原因

java - 是否可以从字体中获取 vector 形式的字符?

maven - 如何从 maven 导入范围中排除 spring-boot-dependencies 的传递依赖

java - 我收到 ERROR TypeError : undefined is not iterable (cannot read property Symbol(Symbol. iterator)) 错误

database - 禁用外键取消引用 Hibernate

java - 如何在 java Google App Engine 中将内容处置设置为内联静态文件

java - Android 位置距离到邻近范围内

java - 发布 : 500 Internal Server Error only in Chrome

java - 如何使用 Spring JPA 在同一事务中的不同数据库上维护多个 sql 查询