如何注入 JpaRepository 的实现

how inject implementation of JpaRepository

我想在 UserService 中使用 UserRepository 中的方法 a,但我得到的是 jpaRepository 而不是我的自定义实现,我应该如何编写 类 来获取它?

存储库:

@Repository
public interface UserRepository<UserEntity extends EntityInterface,Long> extends JpaRepository<UserEntity,Long> {
    Optional<UserEntity> findUserByLogin(String login);
}

带有泛型方法的 CrudAbstractService:

public abstract class CrudAbstractService<ENTITY extends EntityInterface, DTO extends DTOInterface> {
    protected final JpaRepository<ENTITY, Long> jpaRepository;
    protected final Validator<DTO> validator;
    protected final MapperInterface<ENTITY, DTO> mapper;
    private Class<ENTITY> entityClazz;

    public CrudAbstractService(JpaRepository<ENTITY, Long> jpaRepository,
                               Validator<DTO> validator, MapperInterface<ENTITY, DTO> mapper) {
        this.jpaRepository = jpaRepository;
        this.validator = validator;
        this.mapper = mapper;
    }

    public Iterable<DTO> findAll() {
        List<ENTITY> allEntities = jpaRepository.findAll();
        if (allEntities == null) {
            throw new EntityNotFound(entityClazz);
        }
        List<DTO> mappedDTOs = mapper.toDTOs(allEntities);
        return mappedDTOs;
    }

    public void delete(DTO dto) {
        validator.validate(dto);
        ENTITY entity = mapper.toEntity(dto);
        jpaRepository.delete(entity);
    }

    public DTO save(DTO dto) {
        validator.validate(dto);
        ENTITY entity = mapper.toEntity(dto);
        ENTITY save = jpaRepository.save(entity);
        if (save == null) {
            throw new EntityNotFound(entityClazz);
        }
        DTO mappedDTO = mapper.toDTO(save);
        return mappedDTO;
    }

}

CrudUserService 的实现,我想注入 UserRepository 而不是 JpaRepository:

@Service
public class UserService extends CrudAbstractService<UserEntity,UserDTO> {

    private MapperInterface<LectureEntity,LectureDTO> lectureMapper;

    public UserService(UserRepository<UserEntity, Long> jpaRepository,
                       Validator<UserDTO> validator, MapperInterface<UserEntity, UserDTO> mapper,
                       MapperInterface<LectureEntity,LectureDTO> lectureMapper) {
        super(jpaRepository, validator, mapper);
        this.lectureMapper = lectureMapper;
    }


    public UserDTO findUserByLogin(String login) {
        if (login == null) {
            throw new UserNotFoundException();
        }
//Here i want use UserRepository method instead of JpaRepository. 
        Optional<UserEntity> userByLogin = jpaRepository.findUserByLogin(login);
        UserEntity userEntity = userByLogin.orElseThrow(UserNotFoundException::new);
        List<LectureEntity> reservations = userEntity.getReservations();
        List<LectureDTO> lectureDTOS = lectureMapper.toDTOs(reservations);
        UserDTO userDTO = mapper.toDTO(userEntity);
        userDTO.setLectures(lectureDTOS);
        return userDTO;
    }
}

我认为您不需要使存储库接口通用。
因此,替换为:

@Repository
public interface UserRepository<UserEntity extends EntityInterface,Long> extends JpaRepository<UserEntity,Long> {
    Optional<UserEntity> findUserByLogin(String login);
}

有了这个:

@Repository
public interface UserRepository extends JpaRepository<UserEntity,Long> {
    Optional<UserEntity> findUserByLogin(String login);
}

并在您的服务中使用它:

@Service
public class UserService extends CrudAbstractService<UserEntity,UserDTO> {

    private MapperInterface<LectureEntity,LectureDTO> lectureMapper;

    public UserService(UserRepository jpaRepository,
                       Validator<UserDTO> validator, MapperInterface<UserEntity, UserDTO> mapper,
                       MapperInterface<LectureEntity,LectureDTO> lectureMapper) {
        super(jpaRepository, validator, mapper);
        this.lectureMapper = lectureMapper;
    }
}

如果您需要将您的实体映射到 DTO,那么您可以尝试使用 JPA projections

关于在 findAll() 中抛出异常 - 在我看来,这不是一个好主意。您可能 return 只是清空列表,让 class 的客户决定在缺少实体的情况下该怎么做。

同样在你的情况下,我会尽量避免使用抽象 classes 和继承,而是使用组合。 继承与组合:如何选择Why should I prefer composition over inheritance?