java - 无法创建查询元模型 Spring Data JPA 存储库(奇怪的行为)
问题描述
我的 JPA 存储库中出现意外错误。由于与 JPA 相关的异常,Spring 无法创建 repo bean。
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fooRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Could not create query metamodel for method public abstract com.xxxxx.entities.Foo com.xxxxx.repositories.FooRepository.findByCourierId(java.lang.Long)!
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
Caused by: java.lang.IllegalArgumentException: Could not create query metamodel for method public abstract com.xxxxx.entities.Foo com.xxxxx.repositories.FooRepository.findByCourierId(java.lang.Long)!
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:97)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:206)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:73)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:367)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:190)
Caused by: java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [courierId] on this ManagedType [com.xxxxx.core.models.XXXObject]
我的回购代码如下:
public interface FooRepository extends XXXRepository<Foo> {
Foo findByCourierId(Long courierId);
@Query("SELECT CA " +
"FROM Foo CA " +
"WHERE CA.courier.id IN (?1)")
List<Foo> findByCourierIds(List<Long> courierIds);
}
现在奇怪的是,在添加第二种方法之前,它会为第一个查询显示错误,该查询在添加第二种方法之前可以完美找到,如果我删除第二个查询,则代码可以正常工作。
实体的代码是这样的
@Entity
public class Foo extends XXXObject {
@OneToOne
@JoinColumn(name = "courier_id")
private Courier courier;
....
解决方案
代替
public interface FooRepository extends XXXRepository<Foo> {
你可以使用 JPARepository
public interface FooRepository extends JpaRepository<Foo, Long> {
推荐阅读
- reactjs - getState 和 dispatch 如何在 redux-thunk action creator 中导入?
- ms-word - 使用 setSelectedDataAsync 从多个 XML 添加内容
- github - Github 文件夹未完全提交
- eclipse - INFO 日志消息已关闭
- python - 使用管道数据查询 BigQuery apache_beam
- python - 带有系统命令的 STDOUT json 格式
- shutterstock - API 和 Shutterstock 网站中的搜索查询结果不同
- python - ElementClickInterceptedException:消息:元素在点 (x,y) 处不可点击,因为另一个元素遮住了它
- reactjs - react-final-form:使用表单更改返回的数据更新验证规则并重新验证字段
- c# - 有没有办法读取语言配置文件并将其转换为对象?