如何注入JpaRepository的实现

问题描述 投票:0回答:1

我想在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;
    }
}
java spring code-injection
1个回答
0
投票

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

@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()中引发异常-我认为这不是一个好主意。您可能应该只返回一个空列表,并让班级的客户决定在缺少实体的情况下该怎么做。

同样,在您的情况下,我将尽量避免使用抽象类和继承,而应使用合成。 Inheritance versus composition: How to chooseWhy should I prefer composition over inheritance?

© www.soinside.com 2019 - 2024. All rights reserved.