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,
CountPerson
that holds two variables, last_name
and count
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;
}
}
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With