2017-12-05 3 views
0

에서 엔티티를 인터페이스 기반의 투사를 컴파일 할 수 없습니다 엔티티 클래스.내가있어 봄

내 저장소 :

public interface PersonRepository extends JpaRepository<Person, Long> { 

    List<NamesOnly> findAll(); 
} 

나는 그것을 컴파일 할 수 없습니다. 내가 잘못 무엇

test/projectiontest/PersonRepository.java:[20,21] findAll() in test.projectiontest.PersonRepository clashes with findAll() in org.springframework.data.jpa.repository.JpaRepository 
return type java.util.List<test.projectiontest.projection.NamesOnly> is not compatible with java.util.List<test.projectiontest.Person> 

https://docs.spring.io/spring-data/jpa/docs/2.0.2.RELEASE/reference/html/#projections

?

+2

'CrudRepository'에서 특정 서명을 사용하여 이미 정의 된'findAll'을 사용하고 있습니다. 네가 맞지 않아. 다른 메소드 이름이 필요합니다. –

+0

@MDeinum ok, 예 :'List findById (Long id); '는 잘 작동하지만 프로젝션 인터페이스로 findAll을 구현하는 방법은 무엇입니까? – kojot

+0

사용자 정의 방법 정의 ... –

답변

0

글쎄, @ M.Deinum이 언급 한대로 은 이미 JpaRepository에 정의되어 있기 때문에 사용할 수 없습니다.

public class NamesOnlyDto { 

    private String firstname, lastname; 

    public NamesOnlyDto(String firstname, String lastname) { 
     this.firstname = firstname; 
     this.lastname = lastname; 
    } 

    public String getFirstname() { 
     return firstname; 
    } 

    public String getLastname() { 
     return lastname; 
    }  
} 

가 그럼 난 이렇게 내 저장소에 그것을 사용할 수 있습니다 :

@Query("SELECT new test.projectiontest.projection.NamesOnlyDto(p.firstname, p.lastname) FROM Person p") 
List<NamesOnlyDto> findEveryone(); 

그것은 잘 작동 대신 투사 인터페이스를 사용하는 나는 DTO 클래스를 썼다. 나는 이것이 해결 방법이라는 것을 확신하고 프로젝션 인터페이스로 이것을 해결할 수있는 방법이 있으므로이 질문을 남겨 두었다. 나는 다른 누군가가 더 나은 해결책을 제공하기를 바랍니다.