[英]How to use @NamedQuery in spring a CrudRepository @Query?
我想在JpaRepository
使用@NamedQuery
。 但它不起作用:
public interface MyEntityRepository extends JpaRepository<MyEntity, Long> {
@Query(name = MyEntity.FIND_ALL_CUSTOM)
List<MyEntity> findAllCustom(Pageable pageable);
}
@Entity
@NamedQuery(
name = MyEntity.FIND_ALL_CUSTOM, query = "select * from MyEntity me where me.age >= 18"
)
public class MyEntity {
public static final String FIND_ALL_CUSTOM = "findAllCustom";
}
結果:
org.springframework.data.mapping.PropertyReferenceException: No property findAllCustom found for type MyEntity!
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241)
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76)
at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:235)
at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:373)
at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:353)
at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:84)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:61)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:94)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:205)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:72)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:369)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:192)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:239)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:225)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
... 28 more
更新:
public interface MyEntityRepository extends JpaRepository<MyEntity, Long> {
List<MyEntity> findAllCustom(Pageable pageable);
}
@Entity
@NamedQuery(
name = "MyEntity.findAllCustom", query = "select * from MyEntity me where me.age >= 18"
)
public class MyEntity {
}
還是同樣的例外:
PropertyReferenceException: No property findAllCustom found for type MyEntity!
看一下Spring Data JPA的文檔 - 使用JPA NamedQueries。
我建議您遵循文檔中設置的約定(從配置的域類的簡單名稱開始,后跟由點分隔的方法名稱)。 剪切下划線並將查詢命名為
@NamedQuery(name = "MyEntity.findAllCustom", query="...")
甚至更好地添加像findByAge
或某findByAge
這樣的暗示名稱。
要允許執行這些命名查詢,您需要做的就是按如下方式指定MyEntityRepository:
public interface MyEntityRepository extends JpaRepository <MyEntity, Long> {
List<MyEntity> findAllCustom();
}
我用JpaRepository
實現了它,正如文檔所示。 但你可以嘗試一個簡單的CrudRepository
,看看是否有效。
我認為問題在於你使用@Query
和查詢注釋的查詢將優先於使用@NamedQuery定義的查詢。 閱讀@Query用法的文檔 ,我想你在哪里也錯了。
Pageable
,根據這個答案
要應用分頁,必須派生第二個子查詢。 因為子查詢引用相同的字段,所以您需要確保查詢對其引用的實體/表使用別名
這意味着你將重寫你的查詢,如query ="select * from MyEntity me where me.age >= 18"
。
該示例用於@Query
,但這也是一個命名查詢,因此它也應該適用於您的案例。 唯一的區別是使用@Query
實際上直接綁定它們而不是將它們注釋到域類。
更新2
我嘗試了自己的應用程序。 首先,您應該使用別名而不是*(即me
)進行查詢。 其次,你使用FIND_ALL_CUSTOM
的字符串不遵循“MyEntity.findAllCustom”的約定。
復制粘貼:
public interface MyEntityRepository extends JpaRepository<MyEntity, Long> { List<MyEntity> findAllCustom(Pageable pageable); List<MyEntity> findAllCustom(); } @Entity @NamedQuery( name = MyEntity.FIND_ALL_CUSTOM, query = "select me from MyEntity me where me.age >= 18" ) public class MyEntity { public static final String FIND_ALL_CUSTOM = "MyEntity.findAllCustom"; }
兩者都有效。 對於具有可分頁方法參數的那個,將其稱為myEntityRepository.allCustom(new PageRequest(0,20))
。 Ofc,你知道myEntityRepository
是注入的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.