hibernate - 我如何(如果可以)扩展组合存储库?
问题描述
我正在尝试为我的所有“每个聚合根”存储库提供一个通用 Parrent 存储库,以后我可以多态地使用它。
我已经组成了一个父“组合存储库”。这将由其他特定的存储库实现。
@Repository
public interface ZoneNameJpaRepository extends GenericJpaRepository<ZoneDTO, GenericPJM_Id> {
}
public interface GenericJpaRepository<T extends GenericPJM, ID extends GenericPJM_Id>
extends JpaRepository<T, ID>, GenericFindRepository<T, ID> {}
@Repository
public interface GenericFindRepository<T extends GenericPJM, ID extends GenericPJM_Id> {
List<T> findAll(boolean isLazySufficient) throws ClassNotFoundException;
T find(ID id,boolean isLazySufficient) throws ClassNotFoundException;
}
public class GenericFindRepositoryImpl<T extends GenericPJM, ID extends GenericPJM_Id> implements GenericFindRepository<T, ID> {
@Autowired EntityManager entityManager;
@Autowired DTO_NameAndClassMap dto_nameAndClassMap;
private Class<T> genericType;
public GenericFindRepositoryImpl() {
genericType =
(Class<T>) GenericTypeResolver.resolveTypeArgument(getClass(), GenericFindRepository.class);
System.out.println("GenericFindRepositoryImpl Constructor: " + genericType.getSimpleName());
}
@Override
public List<T> findAll(boolean isLazySufficient) throws ClassNotFoundException {
if (isLazySufficient) {
return lazyFindAll();
} else {
return findAll();
}
}
@Override
public T find(ID id, boolean isLazySufficient) throws ClassNotFoundException {
if (isLazySufficient) {
return lazyFind(id);
} else {
return find(id);
}
}
private T find(ID id) {
System.out.println("_________________GenericFIndRepository find_____________________");
return entityManager.find(genericType, id);
}
private T lazyFind(ID id) throws ClassNotFoundException {
EntityGraph entityGraph = entityManager.getEntityGraph("Generic_lazy_loading");
Map hints = new HashMap();
hints.put("javax.persistence.fetchgraph", entityGraph);
System.out.println("_________________GenericFIndRepository find_____________________");
return (T) entityManager.find(genericType, id, hints);
}
private List<T> lazyFindAll() throws ClassNotFoundException {
EntityGraph entityGraph = entityManager.getEntityGraph("Generic_lazy_loading");
List<T> results =
entityManager
.createQuery(getQueryStringToFindAll(), genericType)
.setHint("javax.persistence.fetchgraph", entityGraph)
.getResultList();
System.out.println("_________________GenericFIndRepository lazyFindAll()_____________________");
return results;
}
private List<T> findAll() throws ClassNotFoundException {
EntityGraph entityGraph = entityManager.getEntityGraph("Generic_lazy_loading");
List<T> results =
entityManager.createQuery(getQueryStringToFindAll(), genericType).getResultList();
System.out.println("_________________GenericFIndRepository lazyFindAll()_____________________");
return results;
}
private String getEntityNameBasedOnClass(Class desiredClass) {
// Class<?> aClass = T;
Table table = genericType.getAnnotation(Table.class);
String tableName = table.name();
System.out.println("GENERIC_FIND_REPOSITORY findAllByClass() table name found: " + tableName);
return desiredClass.getSimpleName();
}
private String getQueryStringToFindAll() {
StringBuilder query = new StringBuilder();
query.append("from " + getEntityNameBasedOnClass(genericType));
return query.toString();
}
}
我收到以下错误:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'EDataFeedRestServiceImplV2': Unsatisfied dependency expressed through field 'genericDB_crud_service'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'genericDB_CRUD_ServiceImpl': Unsatisfied dependency expressed through field 'generic_crud_dao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'generic_CRUD_DaoImpl': Unsatisfied dependency expressed through field 'repositoryLookUpMap'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'repositoryLookUpMap': Unsatisfied dependency expressed through field 'zoneNameJpaRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'zoneNameJpaRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.ggktech.pjm.api.model.dto.GenericPJM com.ggktech.pjm.api.repository.repository.generics.GenericFindRepository.findCustom(com.ggktech.pjm.api.model.dto.primaryKeys.GenericPJM_Id,boolean) throws java.lang.ClassNotFoundException! No property findCustom found for type ZoneDTO!
任何解决方法或评论将不胜感激
解决方案
推荐阅读
- c++ - 怎么可能绑定到这个转发函数?
- python - 如果存在于其他列表中,Python 从列表中删除项目
- php - 不同用户的 Laravel 布局
- linux - “smee” webhook 传递客户端停止工作(事件 { 类型:'错误',消息:未定义})?
- css - 验证 (CSS4.0): "overflow-anchor" 是未知的 css 属性名称
- java - 如果属性对 JPA 无效,我如何在数据库中插入默认值
- reactjs - 如何将图像/媒体文件从 React 上传到 Django API?
- android - PowerManager.PARTIAL_WAKE_LOCK 进入休眠状态的前台服务
- php - 使用简码在任何地方显示 WooCommerce 优惠券输入字段并检查值
- google-cloud-platform - 带有 spreadSheetUrl 的 WorksheetFeed 根本不适用于 GCP