Spring Data JPA通过嵌入对象属性查找

126
我希望编写一个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编写查询吗?


6
findByBookIdRegion(Region region, Pageable pageable)难道不能胜任吗? - Oliver Drotbohm
2
是的,就是这样。我找不到关于那个的任何文档。它是否隐藏或隐含在我没有看到的地方? - CorayThan
将其转化为答案,并添加了一个链接到参考文档的相关部分。 - Oliver Drotbohm
5个回答

170

这个方法应该可以解决问题:

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

更多信息请参见参考文档中关于查询派生(query derivation)的部分。


18
如果我有一个 Embeddable 列表或元素集合怎么办? - user962206
如果我们想使用 3 个属性进行 findBy 查询,语法应该是什么? 是类似于 findByPro1AndPro2AndPro3 吗? - surendrapanday
这样的查询是否总是会导致内连接?我正在执行 One findByIdAndTwoId(Long oneId, Long twoId);,它会生成以下形式的查询语句:select ...... from one one_ left outer join two two_ on one_.two_id = two_.id where one_id = ? and two_.id = ? - TroJaN
@surendrapanday:是的,当你有多个列时,findByCol1AndCol2会起作用。 - javatarz

49

上述的 findByBookIdRegion() 对我无效。以下内容适用于最新版本的 String Data JPA:

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

5
你的情况可能与http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-property-expressions中讨论的歧义有关。 - Marcello DeSales

9

如果您正在使用BookId作为联合主键,请记得将您的接口从以下形式更改:

public interface QueuedBookRepo extends JpaRepository<QueuedBook, Long> {

to:

public interface QueuedBookRepo extends JpaRepository<QueuedBook, BookId> {

将您的QueuedBook类中的注释@Embedded更改为@EmbeddedId,如下所示:

public class QueuedBook implements Serializable {

@EmbeddedId
@NotNull
private BookId bookId;

...

1
根据我的经验,Spring并不能轻松处理所有情况。在你的情况下,以下方法应该能解决问题。
Page<QueuedBook> findByBookIdRegion(Region region, Pageable pageable);  

或者

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

但这还取决于您在@Embeddable类中使用的字段命名约定,例如,以下字段可能无法在上述任何风格中正常工作。

private String cRcdDel;

我尝试了以下两种情况,但都没有成功(似乎Spring不能处理这种命名约定,即在开头有太多大写字母,特别是第二个字母(不确定是否仅限于此情况))。
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);

对于两个或运算符,这两个语句是相同的! - Juru

0

有不同的方法可以做到这一点。

  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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接