简体   繁体   English

Spring数据JPA:在结果元组中找不到别名! 执行自定义查询时出错

[英]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. 我正在尝试使用spring数据jpa的@Query注释在mysql数据库上执行自定义查询。

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 和mysql中的查询是

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

While implementing this in Spring data jpa. 在Spring数据jpa中实现这一点。 I'm using this logic, 我正在使用这个逻辑,

  1. create another class CountPerson that holds two variables, last_name and count 创建另一个包含两个变量last_namecount CountPerson
  2. Use @Query to write the query, and the method returns list of object of CountPerson class. 使用@Query编写查询,该方法返回CountPerson类的对象列表。

The query as in spring data jpa is 在spring数据jpa中的查询是

@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 当代码编译并且Web服务器启动正常时,当我尝试运行相关方法时,我得到了

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! 搜索此错误会显示spring数据jpa:在结果元组中找不到别名! 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(...) 差不多......(心连心,所以我希望它是完美的)你需要创建一个新的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. JpaRepository只能轻松返回Person对象,但您可以自己在HQL中创建对象。

A more clean solution is to use Spring Data JPA Projections : 更清晰的解决方案是使用Spring Data JPA Projections

Yo must replace class for a interface and define only get methods: Yo必须替换接口的类并仅定义get方法:

public interface CountPerson {

    String getLastName();

    int getCount();
}

Your Repository method seem like this: 您的Repository方法如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM