无法创建查询元模型spring数据jpa存储库(奇怪的行为)

aor9mmx1  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(307)

我在jpa存储库中遇到了一个意外错误。spring无法创建repobean,因为与jpa相关的异常。

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;
....
bmp9r5qi

bmp9r5qi1#

而不是

public interface FooRepository extends XXXRepository<Foo> {

你能用日语吗

public interface FooRepository extends JpaRepository<Foo, Long> {

相关问题