按嵌入对象属性查找弹簧数据 JPA

2022-08-31 09:00:57

我想编写一个Spring Data JPA存储库接口方法签名,该签名将允许我查找具有该实体中嵌入对象属性的实体。有谁知道这是否可能,如果是,如何?

这是我的代码:

@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为此编写查询吗?


答案 1

此方法名称应该可以解决问题:

Page<QueuedBook> findByBookIdRegion(Region region, Pageable pageable);

有关此内容的更多信息,请参阅有关参考文档的查询派生的部分。


答案 2

上面的 - findByBookIdRegion()对我不起作用。以下内容适用于最新版本的字符串数据 JPA:

Page<QueuedBook> findByBookId_Region(Region region, Pageable pageable);

推荐