how inject implementation of JpaRepository
Asked Answered
F

1

0

I want to use method a from UserRepository in UserService, but I'm getting jpaRepository instead my custom implementation, how should I write classes to get it?

Repository:

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

CrudAbstractService with generics method:

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;
    }

}

Implementation of CrudUserService, there I want to inject UserRepository instead of 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;
    }
}
Fritzfritze answered 9/3, 2020 at 13:12 Comment(3)
You shouldn't design like that. Your CrudAbstractService abstract class is useless, even though you think it's cleverly saving you from writing duplicate code. Now you have a JpaRepository when you need a UserRepository.Colwin
But there is any way to redesign it to get UserRepository?Fritzfritze
Yes, you can redesign it by deleting the abstract class.Colwin
E
0

I think you don't need to make you repository interface generic.
So, replace this:

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

with this:

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

And use it in your service:

@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;
    }
}

If you need to map your entities to DTOs then you can try to use JPA projections

Regarding throwing an exception in findAll() - in my opinion, it's not a good idea. You should probably return just empty list and let the clients of your class decide what to do in case of missing entities.

Also in your case I would try to avoid using abstract classes and inheritance and use composition instead. Inheritance versus composition: How to choose and Why should I prefer composition over inheritance?

Erlineerlinna answered 9/3, 2020 at 13:35 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.