CRUD回购的自定义查询

时间:2017-04-05 14:54:16

标签: hibernate spring-boot spring-data-jpa crud jpql

我有一个表export const environment = { production: false, BASE_TEMPLATE_PATH: 'absolute/path/to/template/file' }; bookreservation,我必须在提供的日期获取保留的书籍详细信息。我在book表中有reservation_dateexpiration_date。我正在使用bookreservation尝试以下自定义查询:

CRUD/JPA Repository

但我收到的错误是:

@Query("from book b where b.bookid in (select br.book_bookid from bookreservation br where br.reservation_date <=:providedDate and br.expiration_date >=:providedDate)") public Iterable<Book> findByReservationDateLessThanAndByExpirationDateGreaterThan(Date providedDate);

请建议我如何使用此获取这些详细信息。 我们可以使用这样的方法:Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: book is not mapped [from book b where b.bookid in (select br.book_bookid from br_reservation br where br.reservation_date <=:providedDate and br.expiration_date >=:providedDate)]

1 个答案:

答案 0 :(得分:0)

你应该使用&#39;加入&#39;在查询中并更正JPQL。像这样:

@Query("select b from BookReservation br join br.book b where br.reservationDate <= ?1 and br.expirationDate >= ?2")
List<Book> findReserved(LocalDate reservationDate, LocalDate expirationDate );

它假设你正在使用这样的实体:

@Entity
@Table(name="book")
public class Book {
    //...
}

@Entity
@Table(name="bookreservation")
public class BookReservation {
    //...
    @ManyToOne
    private Book book;
    private LocalDate reservationDate;
    private LocalDate expirationDate; 
    //...
}