Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring data JPA: getting No aliases found in result tuple! error when executing custom query

I am trying to execute a custom query on the mysql database using the @Query annotation of spring data jpa.

The table is

+------------+---------------+------+-----+---------+-------+
| 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    |       |
+------------+---------------+------+-----+---------+-------+

and the query as in mysql is

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

While implementing this in Spring data jpa. I'm using this logic,

  1. create another class CountPerson that holds two variables, last_name and count
  2. Use @Query to write the query, and the method returns list of object of CountPerson class.

The query as in spring data jpa is

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

While the code compiles and the web server starts fine, when I try to run the related method, I get

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!

Searching for this error shows spring data jpa: No aliases found in result tuple! Make sure your query defines aliases which says that it is a fixed bug. So I guess my issue is different


The codes are

Person class

//imports

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

    @Id
    Long id;
    String firstName;
    String lastName;

    private Person(){}
    //constructor
}

Person repository class

//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 class

@Controller
public class PersonController{

    @Autowired
    PersonRepository repository;

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

Count Person class

public class CountPerson{
    String lastName;
    int count;

    protected CountPerson(){}

    public CountPerson(String lastName,int count){
        this.lastName = lastName;
        this.count = count;
    }
}
like image 520
Registered User Avatar asked Feb 23 '17 15:02

Registered User


2 Answers

Almost there... (by heart, so I hope it's perfect) You'll need to create a new CountPerson(...)

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

The JpaRepository can only easily return Person objects, but you can create objects in HQL yourself.

like image 167
Jeroen van Dijk-Jun Avatar answered Oct 20 '22 08:10

Jeroen van Dijk-Jun


A more clean solution is to use Spring Data JPA Projections:

Yo must replace class for a interface and define only get methods:

public interface CountPerson {

    String getLastName();

    int getCount();
}

Your Repository method seem like this:

@Query("select p.lastName as lastName,count(p.lastName) as count from Person p group by p.lastName")
public List<CountPerson> countbylastname();
like image 7
ajaristi Avatar answered Oct 20 '22 09:10

ajaristi