簡體   English   中英

Spring 數據 JPA 通過嵌入 object 屬性查找

[英]Spring Data JPA find by embedded object property

我想編寫一個 Spring 數據 JPA 存儲庫接口方法簽名,它可以讓我在該實體中找到具有嵌入式 object 屬性的實體。 有誰知道這是否可能,如果可能的話如何?

這是我的代碼:

@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 數據為此編寫查詢嗎?

這個方法名稱應該可以解決問題:

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

有關參考文檔的查詢派生部分的更多信息。

上面的 - findByBookIdRegion()對我不起作用。 以下適用於最新版本的String Data 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;

...

據我所知,Spring並沒有輕松處理所有案件。 在你的情況下,以下應該做的伎倆

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

要么

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

但是,它還取決於您在@Embeddable類中的字段的命名約定,

例如,以下字段可能無法用於上述任何樣式

private String cRcdDel;

我嘗試了兩種情況(如下)並且它不起作用(似乎Spring不處理這種類型的命名約定(即對許多Caps,特別是在開頭 - 第二個字母(不確定是否這是雖然唯一的情況)

Page<QueuedBook> findByBookIdCRcdDel(String cRcdDel, Pageable pageable); 

要么

Page<QueuedBook> findByBookIdCRcdDel(String cRcdDel, Pageable pageable);

當我將列重命名為

private String rcdDel;

我的以下解決方案正常運行沒有任何問題

Page<QueuedBook> findByBookIdRcdDel(String rcdDel, Pageable pageable); 

要么

Page<QueuedBook> findByBookIdRcdDel(String rcdDel, Pageable pageable);

有不同的方法可以做到這一點。

  1. 使用派生方法
  2. 使用本機查詢
  3. 使用規范接口構造復雜的查詢。

下面的代碼片段顯示了如何使用嵌入式屬性查詢數據。

存儲庫擴展了JpaSpecificationExecutor接口。

public interface QuedBookRepository extends JpaRepository<QueuedBook, Long>, JpaSpecificationExecutor<QueuedBook>{
    //Using derived query methods
    Page<QueuedBook> findByBookId_Region(Region region, Pageable pageable);


    //Using native queries
    @Query("SELECT qb FROM QueuedBook qb "
        +"WHERE qb.bookId.region = :region")
    Page<QueuedBook> findByBookIdRegion(Region region, Pageable pageable);
    
    
    //Using specification interface
    static Specification<QueuedBook> findByBookIdRegion(Region region){
        return (root, query, criteriaBuilder) -> criteriaBuilder.equals(root.get("bookId").get("region"), region);
    }
}

要使用存儲庫方法,請按如下方式訪問它們。

//In your service methods

Region region = ....;
Pageable pageable = ....;


//Using derived query methods
Page<QueuedBook> booksPage = repository.findByBookId_Region(region, pageable);

//Using native queries
Page<QueuedBook> booksPage = repository.findByBookIdRegion(region, pageable);

//Using specification interface
Specification<QueuedBook> spec = QuedBookRepository.findByBookIdRegion(region);
Page<QueuedBook> booksPage = repository.findAll(spec, pageable);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM