从Hibernate上的Java lang对象获取价值

时间:2019-04-06 02:07:20

标签: java hibernate

我有疑问:

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:

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"));

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

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

我收到错误消息“无法投射到InvoiceItems”

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

得到这个:

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

但是这样的输出不是值:

[Ljava.lang.Object;@41ea9df8

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

1 个答案:

答案 0 :(得分:1)

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

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;
}