本文介绍了在Hibernate上从Java lang对象获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有疑问:

public List<InvoiceItems> findAllBalance(String external_key) throws HibernateException {
          return (List<InvoiceItems>) session.createQuery("select SUM(i.amount) as amount, t.record_id from Accounts a, InvoiceItems i, Tenant t WHERE a.record_id = i.account_record_id AND t.record_id=a.tenant_record_id  AND a.external_key='"+external_key+"' group by i.tenant_record_id, t.record_id").list();
    }

InvoiceItems.java:

InvoiceItems.java:

package id.co.keriss.consolidate.ee;

import java.util.Date;

import org.jpos.ee.Accounts;

    public class InvoiceItems {

        private long record_id;
        private String id;
        private String type;
        private String invoice_id;
        private Accounts account_record_id;
        private Tenant tenant_record_id;
        private String description;
        private long amount;
        private Date created_date;
        private String usage_name;
        private String plan_name;
        private String account_id;



        public String getAccount_id() {
            return account_id;
        }
        public void setAccount_id(String account_id) {
            this.account_id = account_id;
        }
        public long getRecord_id() {
            return record_id;
        }
        public void setRecord_id(long record_id) {
            this.record_id = record_id;
        }
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getType() {
            return type;
        }
        public void setType(String type) {
            this.type = type;
        }
        public String getInvoice_id() {
            return invoice_id;
        }
        public void setInvoice_id(String invoice_id) {
            this.invoice_id = invoice_id;
        }
        public Accounts getAccount_record_id() {
            return account_record_id;
        }
        public void setAccount_record_id(Accounts account_record_id) {
            this.account_record_id = account_record_id;
        }
        public Tenant getTenant_record_id() {
            return tenant_record_id;
        }
        public void setTenant_record_id(Tenant tenant_record_id) {
            this.tenant_record_id = tenant_record_id;
        }
        public String getDescription() {
            return description;
        }
        public void setDescription(String description) {
            this.description = description;
        }
        public long getAmount() {
            return amount;
        }
        public void setAmount(long amount) {
            this.amount = amount;
        }
        public Date getCreated_date() {
            return created_date;
        }
        public void setCreated_date(Date created_date) {
            this.created_date = created_date;
        }
        public String getUsage_name() {
            return usage_name;
        }
        public void setUsage_name(String usage_name) {
            this.usage_name = usage_name;
        }
        public String getPlan_name() {
            return plan_name;
        }
        public void setPlan_name(String plan_name) {
            this.plan_name = plan_name;
        }


    }

然后将其设置为变量:

List<InvoiceItems> invoiceItems = invoiceItemsDao.findAllBalance(jsonRecv.getString("externalkey"));

我想从该查询中获取值,我可以尝试以下方法:

I want to get the value from that query, what I try:

LogSystem.info(request, "List : " + invoiceItems.get(0).getId();

我遇到了无法转换为InvoiceItems"的错误

I got an error "can't cast to InvoiceItems"

然后我尝试从 List< InvoiceItems> 更改为仅 List

then I try changing from List<InvoiceItems> to just List

得到这个:

LogSystem.info(request, "List : " + invoiceItems.get(0);

但是这样的输出不是值:

but the output like this not the value:

[Ljava.lang.Object;@41ea9df8

有什么建议吗?我想要的最终结果是计算租户数量

any advice? final result what i want is calculate amount of tenant

推荐答案

session.createQuery采用HQL(休眠查询语言),但是我看到您使用本机SQL.尝试使用createSQLQuery方法.

session.createQuery take HQL (Hibernate query language) however I see you use native SQL. Try using createSQLQuery method.

public List<InvoiceItems> findAllBalance(String external_key) throws HibernateException {
     Query query = session.createSQLQuery("select SUM(i.amount) as amount, t.record_id from Accounts a, InvoiceItems i, Tenant t WHERE a.record_id = i.account_record_id AND t.record_id=a.tenant_record_id  AND a.external_key='"+external_key+"' group by i.tenant_record_id, t.record_id");
    query.setResultTransformer(Transformers.aliasToBean(InvoiceItems.class));
     List<InvoiceItems> list = query.list();
        return list;
}

这篇关于在Hibernate上从Java lang对象获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:47