我(如果可以)如何扩展组成的存储库?

时间:2019-04-22 13:34:43

标签: hibernate jpa spring-data-jpa

我正在尝试将通用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!

任何解决方法或评论将不胜感激

0 个答案:

没有答案