【发布时间】:2017-09-22 11:53:52
【问题描述】:
我想向我的所有存储库介绍<T> T findOrCreate(Supplier<Optional<T>> finder, Supplier<T> factory)。
于是新建了一个界面
@NoRepositoryBean
public interface ExtendedJpaRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
T findOrCreate(Supplier<Optional<T>> finder, Supplier<T> factory);
}
.
public class ExtendedJpaRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements ExtendedJpaRepository<T, ID> {
private final JpaEntityInformation entityInformation;
private final EntityManager entityManager;
public ExtendedJpaRepositoryImpl(JpaEntityInformation entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
this.entityInformation = entityInformation;
this.entityManager = entityManager;
}
@Override
public T findOrCreate(Supplier<Optional<T>> finder, Supplier<T> factory) {
throw new NotImplementedException("No implemented yet");
}
}
然后我在我的具体存储库中使用这个接口,例如配方成分库:
public interface RecipeIngredientRepository extends ExtendedJpaRepository<RecipeIngredient, Long> {}
当我最终将存储库注入我的服务时,我得到以下异常:
java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'recipeIngredientRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property find found for type RecipeIngredient! Did you mean 'id'?
它正在我的实体RecipeIngredient 中搜索find 属性。我不希望它这样做。我认为这与JPA Query Methods 有关。所以我将名称从findOrCreate 更改为xxx 以绕过任何查询方法检测 - 没有成功。然后它会搜索xxx 属性。
是什么让 spring data 寻找这个属性?
我正在使用org.springframework.boot:spring-boot-starter-data-jpa。
【问题讨论】:
标签: spring-boot spring-data spring-repositories