簡體   English   中英

Spring Data自定義方法錯誤:org.springframework.data.mapping.PropertyReferenceException:找不到類型的屬性xxx

[英]Spring Data Custom Method Error : org.springframework.data.mapping.PropertyReferenceException: No property xxx found for type

我正在嘗試創建基礎存儲庫類以添加新的自定義方法,但是會出錯。

@NoRepositoryBean
public interface XRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
Optional<T> xxx(ID id);
}

public class XRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements XRepository<T, ID> {

    private final EntityManager entityManager;

    public XRepositoryImpl(JpaEntityInformation entityInformation, EntityManager entityManager) {
        super(entityInformation, entityManager);
    }

    @Override
    public Optional<T> xxx(ID id) {
        return Optional.ofNullable(findOne(id));
    }
}

public interface BookRepository extends XRepository<Book, Long> {
}

堆棧跟蹤:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property xxx found for type Book!
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:77) ~[spring-data-commons-1.12.6.RELEASE.jar:na]
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329) ~[spring-data-commons-1.12.6.RELEASE.jar:na]
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309) ~[spring-data-commons-1.12.6.RELEASE.jar:na]

我檢查了相同問題的答案,但沒有幫助。 他們指出了命名約定,我看不出任何問題。

Spring數據mongo按照類的命名約定工作。 如果您的存儲庫接口名為XXXRepository,則自定義存儲庫接口應命名為XXXRespositoryCustom(RepositoyName + Custom),而實現類應命名為XXXRepositoryImpl。 只有這樣,您的自定義實現才會被讀取。

您很親密,但是您的班級命名是導致您出現問題的原因。 嘗試這個:

public interface XRepositoryOperations<T, ID extends Serializable> {
    Optional<T> xxx(ID id);
}

public interface BookRepository extends JpaRepository<Book, Long>, XRepositoryOperations<Book, Long> {
}

public class XRepositoryImpl<T, ID extends Serializable> implements XRepositoryOperations<T, ID> {

    @Autowired private BookRepository bookRepository;

    @Override
    public Optional<T> xxx(ID id) {
        return Optional.ofNullable(bookRepository.findOne(id));
    }
}

使用當前的類定義,Spring將從BookRepository到XRepository,作為將用於實現API的類。 它正在嘗試使用其method-name-to-query轉換實現您的xxx方法。 如果要從方法自動實現中排除方法,則需要在單獨的接口中定義它們,然后使存儲庫接口對其進行擴展。 這是將在您定義的Impl存儲庫類中實現的類。

編輯

如果您正在使用Java 8,並且希望將所有內容保留在基類中,那么另一個選擇是提供xxx方法的默認實現:

public interface XRepository<T, ID extends Serializable> extends JpaRepository {

    default Optional<T> xxx(ID id) {
        return Optional.ofNullable(this.findOne(id));
    }

}

正如@Manish所指出的,我缺少一個配置:

使用@EnableJpaRepositories使Spring Data基礎結構了解定制的存儲庫基類

@Configuration
@EnableJpaRepositories(repositoryBaseClass = XRepositoryImpl.class)
class ApplicationConfiguration { … }

您正在使用Spring Data JPA。 您必須在存儲庫中使用@Repository 並在您的ApplicationContext.xml中使用以下代碼:

<context:component-scan annotation-config="true"
    base-package="com.demo.test" />

<context:annotation-config />
<jpa:repositories base-package="com.demo.test.repository" />
<tx:annotation-driven transaction-manager="transactionManager" />

暫無
暫無

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

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