簡體   English   中英

無法創建查詢元模型Spring Data JPA存儲庫(奇怪的行為)

[英]Could not create query metamodel Spring Data JPA repository (Weird behaviour)

我的JPA存儲庫中出現意外錯誤。 由於與JPA相關的異常,Spring無法創建回購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> {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM