복붙노트

[SPRING] Spring 데이터 JPA : get 결과 튜플에서 별칭을 찾지 못했습니다! 사용자 지정 쿼리를 실행할 때의 오류

SPRING

Spring 데이터 JPA : get 결과 튜플에서 별칭을 찾지 못했습니다! 사용자 지정 쿼리를 실행할 때의 오류

Spring 데이터 jpa의 @Query 주석을 사용하여 mysql 데이터베이스에서 사용자 지정 쿼리를 실행하려고합니다.

테이블은

+------------+---------------+------+-----+---------+-------+
| Field      | Type          | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| id         | decimal(10,0) | NO   | PRI | NULL    |       |
| first_name | varchar(20)   | YES  |     | NULL    |       |
| last_name  | varchar(20)   | YES  |     | NULL    |       |
+------------+---------------+------+-----+---------+-------+

그리고 mysql에서와 같은 쿼리

select last_name,count(last_name) as count from person group by last_name;

Spring 데이터 jpa에서 이것을 구현하는 동안. 나는이 논리를 사용하고있다.

봄 데이터 jpa의 쿼리는 다음과 같습니다.

@Query("select p.lastName,count(p.lastName) as count from Person p group by p.lastName")

코드가 컴파일되고 웹 서버가 시작될 때 관련 메소드를 실행하려고하면

There was an unexpected error (type=Internal Server Error, status=500).
No aliases found in result tuple! Make sure your query defines aliases!; nested exception is java.lang.IllegalStateException: No aliases found in result tuple! Make sure your query defines aliases!

이 오류를 검색하면 봄 데이터가 표시됩니다. jpa : 결과 튜플에 별명이 없습니다! 쿼리가 고정 된 버그라는 별칭을 정의하는지 확인하십시오. 그래서 내 문제는 다른 것 같아.

코드는

Person 클래스

//imports

@Entity
@Table(name = "person")
public class Person{

    @Id
    Long id;
    String firstName;
    String lastName;

    private Person(){}
    //constructor
}

Person 저장소 클래스

//imports
@Transactional
public interface PersonRepository extends CrudRepository<Person,Long>{

    @Query("select p.lastName,count(p.lastName) as count from Person p group by p.lastName")
    public List<CountPerson> countbylastname();
}

컨트롤러 클래스

@Controller
public class PersonController{

    @Autowired
    PersonRepository repository;

    @RequestMapping("/count")
    @ResponseBody
    public List<CountPerson> countbylastname(){
        return repository.countbylastname();
    }
}

사람 수를 세어 라.

public class CountPerson{
    String lastName;
    int count;

    protected CountPerson(){}

    public CountPerson(String lastName,int count){
        this.lastName = lastName;
        this.count = count;
    }
}

해결법

  1. ==============================

    1.거의 다 ... (마음으로, 그것이 완벽하길 바래요) 당신은 새로운 백작 인 (...)을 만들어야합니다.

    거의 다 ... (마음으로, 그것이 완벽하길 바래요) 당신은 새로운 백작 인 (...)을 만들어야합니다.

    select new com.mypackage.CountPerson(p.last_name, count(p.last_name)) from person p ... 
    

    JpaRepository는 Person 객체 만 쉽게 반환 할 수 있지만 HQL에서 객체를 직접 만들 수 있습니다.

  2. ==============================

    2.더 깨끗한 해결책은 Spring Data JPA Projections를 사용하는 것입니다.

    더 깨끗한 해결책은 Spring Data JPA Projections를 사용하는 것입니다.

    Yo는 인터페이스의 클래스를 대체하고 get 메소드 만 정의해야합니다.

    public interface CountPerson {
    
        String getLastName();
    
        int getCount();
    }
    

    저장소 메소드는 다음과 같습니다.

    @Query("select p.lastName as lastName,count(p.lastName) as count from Person p group by p.lastName")
    public List<CountPerson> countbylastname();
    
  3. from https://stackoverflow.com/questions/42419305/spring-data-jpa-getting-no-aliases-found-in-result-tuple-error-when-executing by cc-by-sa and MIT license