I want to write a Spring Data JPA repository interface method signature that will let me find entities with a property of an embedded object in that entity. Does anyone know if this is possible, and if so how?
我想编写一个Spring Data JPA repository接口方法签名,它将让我在该实体中找到具有嵌入对象属性的实体。有谁知道这是否可能,如果是,如何?
Here's my code:
这是我的代码:
@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);
}
Can I write a query for this using Spring Data?
我能用Spring数据编写查询吗?
3 个解决方案
#1
92
This method name should do the trick:
这个方法的名称应该可以做到:
Page<QueuedBook> findByBookIdRegion(Region region, Pageable pageable);
More info on that in the section about query derivation of the reference docs.
关于参考文档的查询派生部分的更多信息。
#2
29
The above - findByBookIdRegion() did not work for me. The following works with the latest release of String Data JPA:
以上- findByBookIdRegion()对我不起作用。以下是最新发布的字符串数据JPA:
Page<QueuedBook> findByBookId_Region(Region region, Pageable pageable);
#3
7
If you are using BookId as an combined primary key, then remember to change your interface from:
如果您使用BookId作为组合主键,那么请记住更改您的接口:
public interface QueuedBookRepo extends JpaRepository<QueuedBook, Long> {
to:
:
public interface QueuedBookRepo extends JpaRepository<QueuedBook, BookId> {
And change the annotation @Embedded to @EmbeddedId, in your QueuedBook class like this:
并将@嵌入到@Embedded dedid中,在您的QueuedBook类中:
public class QueuedBook implements Serializable {
@EmbeddedId
@NotNull
private BookId bookId;
...
}
#1
92
This method name should do the trick:
这个方法的名称应该可以做到:
Page<QueuedBook> findByBookIdRegion(Region region, Pageable pageable);
More info on that in the section about query derivation of the reference docs.
关于参考文档的查询派生部分的更多信息。
#2
29
The above - findByBookIdRegion() did not work for me. The following works with the latest release of String Data JPA:
以上- findByBookIdRegion()对我不起作用。以下是最新发布的字符串数据JPA:
Page<QueuedBook> findByBookId_Region(Region region, Pageable pageable);
#3
7
If you are using BookId as an combined primary key, then remember to change your interface from:
如果您使用BookId作为组合主键,那么请记住更改您的接口:
public interface QueuedBookRepo extends JpaRepository<QueuedBook, Long> {
to:
:
public interface QueuedBookRepo extends JpaRepository<QueuedBook, BookId> {
And change the annotation @Embedded to @EmbeddedId, in your QueuedBook class like this:
并将@嵌入到@Embedded dedid中,在您的QueuedBook类中:
public class QueuedBook implements Serializable {
@EmbeddedId
@NotNull
private BookId bookId;
...
}