我正在尝试使用Spring Data实现自定义存储库,但出现以下异常:
原因:org.springframework.data.mapping.PropertyReferenceException:未找到类型为{Entity}的属性{name}
看起来像 @NoRepositoryBean 不能正常工作,并且Spring正在扫描带有注释的接口,请问有人知道如何以正确的方式进行操作吗?
所有存储库的自定义方法。
@NoRepositoryBean
public interface BaseRepository<T, ID> extends JpaRepository<T, ID> {
List<T> findAll(Company company);
EntityManager getEntityManager();
}
public class BaseRepositoryImpl<T, ID> extends SimpleJpaRepository<T, ID> implements BaseRepository<T, ID> {
private EntityManager entityManager;
public BaseRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
this.entityManager = entityManager;
}
public EntityManager getEntityManager() {
return entityManager;
}
public List<T> findAll(Company company) {
//Search by company
}
}
我也有一些具有自定义方法的存储库,例如:
@NoRepositoryBean
public interface QuestionCustomRepository {
List<Question> findAllQuestionByTypeAndAsset(Integer[] assetIds);
}
public interface QuestionRepository extends BaseRepository<Question, Integer>, QuestionCustomRepository {
}
@Repository
public class QuestionCustomRepositoryImpl extends BaseRepositoryImpl<Question, Integer> implements QuestionCustomRepository {
public QuestionCustomRepositoryImpl(JpaEntityInformation<Question, ?> entityInformation, EntityManager entityManager) {
super(entityInformation, entityManager);
}
@Override
public List<Question> findAllQuestionByTypeAndAsset(Integer[] assetIds) {
EntityManager entityManager = getEntityManager();
// Call Native Query
}
}
测试
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBaseTest.SpringConfiguration.class, properties = "logging.level.org.springframework.data=DEBUG")
@DataJpaTest
public class SpringBaseTest {
@SpringBootApplication
@EntityScan(basePackageClasses = Question.class)
@EnableJpaRepositories(basePackageClasses = QuestionRepository.class, repositoryBaseClass = BaseRepositoryImpl.class)
public static class SpringConfiguration {
}
}
class QuestionRepository extends SpringBaseTest {
@Inject
QuestionRepository questionRepository;
@Test
public void testFindAllByAssetIts() throws Exception {
List<Question> list = questionRepository.findAllQuestionByTypeAndAsset(new Integer[]{1,2,3});
assertThat(list).hasSize(3);
}
}
感谢您的帮助。
答案 0 :(得分:0)
解决了问题,从界面上删除了@NoRepositoryBean
,只留了BaseRepository
。
还需要删除@SpringBootTest
,因为已经在使用@DataJpaTest
。