繁体   English   中英

如何在Spring中使用@NamedQuery一个CrudRepository @Query?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM