Spring Data JPA通过embedded对象属性来查找
我想编写一个Spring Data JPA存储库接口方法签名,它使我能够在该实体中find具有embedded对象属性的实体。 有谁知道这是可能的,如果是的话,怎么样?
这是我的代码:
@Entity @Table(name = "BOOK_UPDATE_QUEUE", indexes = { uniqueConstraints = @UniqueConstraint(columnNames = { "bookId", "region" }, name = "UK01_BOOK_UPDATE_QUEUE")) public class QueuedBook implements Serializable { @Embedded @NotNull private BookId bookId; ... } @Embeddable public class BookId implements Serializable { @NotNull @Size(min=1, max=40) private String bookId; @NotNull @Enumerated(EnumType.STRING) private Region region; ... } public interface QueuedBookRepo extends JpaRepository<QueuedBook, Long> { //I'd like to write a method like this, but can't figure out how to search by region, //when region is actually a part of the embedded BookId Page<QueuedBook> findByRegion(Region region, Pageable pageable); }
我可以使用Spring Data为此编写一个查询吗?
这个方法的名字应该做的伎俩:
Page<QueuedBook> findByBookIdRegion(Region region, Pageable pageable);
有关参考文档的查询派生部分的更多信息。
以上 – findByBookIdRegion()不适合我。 以下内容适用于最新版本的string数据JPA:
Page<QueuedBook> findByBookId_Region(Region region, Pageable pageable);
如果您将BookId作为组合主键使用,请记住从以下位置更改界面:
public interface QueuedBookRepo extends JpaRepository<QueuedBook, Long> {
至:
public interface QueuedBookRepo extends JpaRepository<QueuedBook, BookId> {
在你的QueuedBook类中更改注释@Embedded到@EmbeddedId,如下所示:
public class QueuedBook implements Serializable { @EmbeddedId @NotNull private BookId bookId; ... }