Как создать hql-запрос для выбора и группировки в Hibernate - PullRequest
0 голосов
/ 19 апреля 2019

Я хочу создать hql-запрос для результата возврата с оператором select и group by. Я знаю, как написать SQL-запрос в PostgreSQL, и могу вернуть свой результат, но я не могу вернуть этот результат с помощью hql в своем классе обслуживания.

Надеюсь, вы мне поможете, спасибо.

Это SQL-запрос в PostgreSQL, который возвращает мой результат:

select 
    trs.device_id, trs.op_type, 
    count(id) as op_count, 
    sum(price) as op_sum, sum(tr_comm) as op_total_com 
from 
    trs 
group by 
    device_id, op_type

Как я могу вернуть тот же результат с HQL?

Это мой класс сущности

@Entity
@Table(name = "trs")
public class infoExcel {

    @Id
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "entity_id_seq"
    )
    @SequenceGenerator(
            name = "entity_id_seq",
            sequenceName = "global_id_sequence",
            allocationSize = 1
    )
    @Column(
            name = "id",
            unique = true,
            updatable = false,
            nullable = false
    )
    private Long id;
    @Column(name = "rule_id",insertable = false,updatable = false)
    private Integer rule_id;
    private String device_id;
    private String ts_date;
    private String ts_time;
    private String op_type;
    private String detail_pay;
    private String card_num;
    private String ref_num;
    private String tr_code;
    private Integer price;
    private Integer month_code;
    private Long tr_comm;

    @ManyToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
    @JoinColumn(name = "rule_id")
    private Rule rule_trs;

    public infoExcel() {
    }

    public infoExcel(Integer rule_id, String device_id, String ts_date, String ts_time, String op_type, String detail_pay, String card_num, String ref_num, String tr_code, Integer price, Integer month_code, Long tr_comm) {
        this.rule_id = rule_id;
        this.device_id = device_id;
        this.ts_date = ts_date;
        this.ts_time = ts_time;
        this.op_type = op_type;
        this.detail_pay = detail_pay;
        this.card_num = card_num;
        this.ref_num = ref_num;
        this.tr_code = tr_code;
        this.price = price;
        this.month_code = month_code;
        this.tr_comm = tr_comm;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getRule_id() {
        return rule_id;
    }

    public void setRule_id(Integer rule_id) {
        this.rule_id = rule_id;
    }

    public String getDevice_id() {
        return device_id;
    }

    public void setDevice_id(String device_id) {
        this.device_id = device_id;
    }

    public String getTs_date() {
        return ts_date;
    }

    public void setTs_date(String ts_date) {
        this.ts_date = ts_date;
    }

    public String getTs_time() {
        return ts_time;
    }

    public void setTs_time(String ts_time) {
        this.ts_time = ts_time;
    }

    public String getOpr_type() {
        return op_type;
    }

    public void setOpr_type(String opr_type) {
        this.op_type = opr_type;
    }

    public String getDetail_pay() {
        return detail_pay;
    }

    public void setDetail_pay(String detail_pay) {
        this.detail_pay = detail_pay;
    }

    public String getCard_number() {
        return card_num;
    }

    public void setCard_number(String card_number) {
        this.card_num = card_number;
    }

    public String getReference_number() {
        return ref_num;
    }

    public void setReference_number(String reference_number) {
        this.ref_num = reference_number;
    }

    public String getTs_code() {
        return tr_code;
    }

    public void setTs_code(String ts_code) {
        this.tr_code = ts_code;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    public Integer getMonth_code() {
        return month_code;
    }

    public void setMonth_code(Integer month_code) {
        this.month_code = month_code;
    }

    public Long getTs_com() {
        return tr_comm;
    }

    public void setTs_com(Long ts_com) {
        this.tr_comm = ts_com;
    }

    public Rule getRule_trs() {
        return rule_trs;
    }

    public void setRule_trs(Rule rule_trs) {
        this.rule_trs = rule_trs;
    }

    @Override
    public String toString() {
        return "infoExcel{" +
                "device_id='" + device_id + '\'' +
                ", ts_date='" + ts_date + '\'' +
                ", ts_time='" + ts_time + '\'' +
                ", opr_type='" + op_type + '\'' +
                ", card_number='" + card_num + '\'' +
                ", reference_number='" + ref_num + '\'' +
                ", ts_code='" + tr_code + '\'' +
                ", price='" + price + '\'' +
                ", month_code=" + month_code +
                ", ts_com=" + tr_comm +
                '}';
    }
}

и мой класс репо:

@Repository
@Transactional
public class DeviceSumCalcImpl implements DeviceSumCalc {

    @Autowired
    private SessionFactory sessionFactory;

    public void saveDeviceSum(DeviceSum deviceSum) {

    }

    public List<DeviceSum> getDeviceSum() {

        String Query = "select infoExcel.device_id,infoExcel.op_type,count(infoExcel.id) as op_count,sum(infoExcel.price) as op_sum, sum(infoExcel.tr_comm) as op_total_com from infoExcel group by device_id,op_type";
        Session session = sessionFactory.getCurrentSession();
        Query query = session.createQuery(Query);
        List<DeviceSum> deviceSums = query.list();

        return deviceSums;
    }
}

и мой класс DeviceSum:

@Entity
@Table(name = "device_sum")
public class DeviceSum {

    @Id
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "entity_id_seq"
    )
    @SequenceGenerator(
            name = "entity_id_seq",
            sequenceName = "entity_id_seq",
            allocationSize = 1
    )
    @Column(
            name = "id",
            unique = true,
            updatable = false,
            nullable = false
    )
    private Long id;
    @Column(name = "device_id")
    private String DeviceId;
    @Column(name = "op_type")
    private String operationType;
    @Column(name = "op_count")
    private Integer operationCount;
    @Column(name = "op_sum")
    private Integer operationSum;
    @Column(name = "op_total_com")
    private Integer operationTotalCommission;
    @Column(name = "month_code")
    private Integer monthCode;
    @Column(name = "rule_id",insertable = false,updatable = false)
    private Long RuleId;
    @Column(name = "op_com1")
    private Integer operationComm1;
    @Column(name = "op_com2")
    private Integer operationComm2;
    @Column(name = "op_com3")
    private Integer operationComm3;
    @Column(name = "op_com4")
    private Integer operationComm4;

    @ManyToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
    @JoinColumn(name = "rule_id")
    private Rule rule_ds;

    public DeviceSum() {
    }

    public DeviceSum(Long id, String deviceId, String operationType, Integer operationCount, Integer operationSum, Integer operationTotalCommission, Integer monthCode, Long ruleId, Integer operationComm1, Integer operationComm2, Integer operationComm3, Integer operationComm4) {
        this.id = id;
        DeviceId = deviceId;
        this.operationType = operationType;
        this.operationCount = operationCount;
        this.operationSum = operationSum;
        this.operationTotalCommission = operationTotalCommission;
        this.monthCode = monthCode;
        RuleId = ruleId;
        this.operationComm1 = operationComm1;
        this.operationComm2 = operationComm2;
        this.operationComm3 = operationComm3;
        this.operationComm4 = operationComm4;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getDeviceId() {
        return DeviceId;
    }

    public void setDeviceId(String deviceId) {
        DeviceId = deviceId;
    }

    public String getOperationType() {
        return operationType;
    }

    public void setOperationType(String operationType) {
        this.operationType = operationType;
    }

    public Integer getOperationCount() {
        return operationCount;
    }

    public void setOperationCount(Integer operationCount) {
        this.operationCount = operationCount;
    }

    public Integer getOperationSum() {
        return operationSum;
    }

    public void setOperationSum(Integer operationSum) {
        this.operationSum = operationSum;
    }

    public Integer getOperationTotalCommission() {
        return operationTotalCommission;
    }

    public void setOperationTotalCommission(Integer operationTotalCommission) {
        this.operationTotalCommission = operationTotalCommission;
    }

    public Integer getMonthCode() {
        return monthCode;
    }

    public void setMonthCode(Integer monthCode) {
        this.monthCode = monthCode;
    }

    public Long getRuleId() {
        return RuleId;
    }

    public void setRuleId(Long ruleId) {
        RuleId = ruleId;
    }

    public Integer getOperationComm1() {
        return operationComm1;
    }

    public void setOperationComm1(Integer operationComm1) {
        this.operationComm1 = operationComm1;
    }

    public Integer getOperationComm2() {
        return operationComm2;
    }

    public void setOperationComm2(Integer operationComm2) {
        this.operationComm2 = operationComm2;
    }

    public Integer getOperationComm3() {
        return operationComm3;
    }

    public void setOperationComm3(Integer operationComm3) {
        this.operationComm3 = operationComm3;
    }

    public Integer getOperationComm4() {
        return operationComm4;
    }

    public void setOperationComm4(Integer operationComm4) {
        this.operationComm4 = operationComm4;
    }

    public Rule getRule_ds() {
        return rule_ds;
    }

    public void setRule_ds(Rule rule_ds) {
        this.rule_ds = rule_ds;
    }
}

Как создать HQL-запрос для возврата результата в методе getDeviceSum класса DeviceSumCalcImpl?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...