将结果从HQL转换为对象

时间:2018-08-17 14:01:03

标签: java hibernate spring-boot spring-data-jpa hql

这是我在存储库类中的方法:

@Query("select max(entry_fee) as maxBuyInFee, max(prize) as maxPrize, max(participants_number) as maxParticipants from Tournament tournament")
    FilterMaxValues findFilterMaxValues();

这是我的FilterMaxValues类:

public class FilterMaxValues {

    private Integer maxBuyInFee;

    private Integer maxPrize;

    private Integer maxParticipants;

如何将此HQL的结果转换为FilterMaxValues对象?

现在我得到了:

  

未找到能够从类型转换的转换器   [java.util.HashMap]键入[com.test.FilterMaxValues]

2 个答案:

答案 0 :(得分:1)

如果您的FilterMaxValues不是实体(例如,

),则可以使用projections
interface FilterMaxValues {

Integer getMaxBuyInFee();

Integer getMaxPrize();

Integer getMaxParticipants();

}

答案 1 :(得分:0)

您是否不需要用FilterMaxValues@Entity来注释@Table(name="yourDBTable")类,以便Spring可以自动为您进行转换?