Java Spring Data JPA 通过嵌入对象属性查找
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24441411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Data JPA find by embedded object property
提问by CorayThan
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 存储库接口方法签名,它可以让我在该实体中找到具有嵌入对象属性的实体。有谁知道这是否可能,如果可能,如何?
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 Data 为此编写查询吗?
采纳答案by Oliver Drotbohm
This method name should do the trick:
这个方法名称应该可以解决问题:
Page<QueuedBook> findByBookIdRegion(Region region, Pageable pageable);
More info on that in the section about query derivationof the reference docs.
有关参考文档的查询派生部分的更多信息。
回答by Bazinga
The above - findByBookIdRegion() did not work for me. The following works with the latest release of String Data JPA:
以上 - findByBookIdRegion() 对我不起作用。以下适用于最新版本的 String Data JPA:
Page<QueuedBook> findByBookId_Region(Region region, Pageable pageable);
回答by Tim Kara
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 更改为@EmbeddedId,在您的 QueuedBook 类中,如下所示:
public class QueuedBook implements Serializable {
@EmbeddedId
@NotNull
private BookId bookId;
...
回答by Bhavik
According to me, Spring doesn't handle all the cases with ease. In your case the following should do the trick
在我看来,Spring 并不能轻松处理所有情况。在您的情况下,以下内容应该可以解决问题
Page<QueuedBook> findByBookIdRegion(Region region, Pageable pageable);
or
或者
Page<QueuedBook> findByBookId_Region(Region region, Pageable pageable);
However, it also depends on the naming convention of fields that you have in your @Embeddable
class,
但是,这也取决于您在@Embeddable
类中拥有的字段的命名约定,
e.g. the following field might not work in any of the styles that mentioned above
例如,以下字段可能不适用于上述任何样式
private String cRcdDel;
I tried with both the cases (as follows) and it didn't work (it seems like Spring doesn't handle this type of naming conventions(i.e. to many Caps , especially in the beginning - 2nd letter (not sure about if this is the only case though)
我尝试了这两种情况(如下所示),但没有奏效(似乎 Spring 不处理这种类型的命名约定(即许多 Caps ,尤其是在开头 - 第二个字母(不确定这是否是唯一的情况)
Page<QueuedBook> findByBookIdCRcdDel(String cRcdDel, Pageable pageable);
or
或者
Page<QueuedBook> findByBookIdCRcdDel(String cRcdDel, Pageable pageable);
When I renamed column to
当我将列重命名为
private String rcdDel;
my following solutions work fine without any issue:
我的以下解决方案工作正常,没有任何问题:
Page<QueuedBook> findByBookIdRcdDel(String rcdDel, Pageable pageable);
OR
或者
Page<QueuedBook> findByBookIdRcdDel(String rcdDel, Pageable pageable);