Ljava.lang.Object;无法转换为模型

时间:2018-07-13 09:17:12

标签: java hibernate pojo

在这种情况下,我想显示Json到java休眠的响应页面中,像这样从DAO查询方法:

public List<TransactionQR> getAllTransaction() throws HibernateException {
        return this.session.createQuery("FROM TransactionQR tr, Batch b, Terminal t, User_Smartphone us, Merchant mc WHERE tr.batch = b.id AND b.user_smartphone = us.id AND b.terminal = t.id AND t.merchant = mc.id AND state = '1' ").list();
    }

然后在servlet类中,我尝试使用Json对象和json数组将列表转换为json,然后像这样写响应:

            int start = 0;
            String jsonResult = null;
            JSONObject jo=new JSONObject();

            HttpServletRequest request = context.getRequest();
            HttpServletResponse response = context.getResponse();

            HttpSession session = context.getSession();

            DB db = getDB(context);

            //JSONObject jo = new JSONObject();
            QRTransactionDao QR = new QRTransactionDao(db);
            //Gson objGson = new GsonBuilder().setPrettyPrinting().create();
            //String json = objGson.toJson(QR.getAllTransaction());

            //System.out.println(json);

            List<TransactionQR> str = QR.getAllTransaction();    
            JSONArray array = new JSONArray();

            for(TransactionQR tr : str){

                 JSONObject str3 = new JSONObject();
                 str3.put("amount", tr.getAmount());
                 context.put("jsoncontent", jsonResult);

                 array.add(str3);
            }

            jo.put("status", "ok");
            jo.put("dataqr", array);

            jsonResult=jo.toString();
            response.setContentType("application/json");
            response.getWriter().print(jsonResult);

但是我在此行循环中收到语法错误:

for(TransactionQR tr : str){

这样的错误:

  

[Ljava.lang.Object;无法转换为交易

此处为交易模型:

package id.co.keriss.consolidate.ee;

import java.io.Serializable;
import java.util.Date;
public class TransactionQR implements Serializable{
    private Long id;
    private String codeqr;
    private Date approvaltime;
    private String merchant;
    private String code_merchant;
    private Long amount;
    private Long saldoawal;
    private Integer tracenumber;
    private String state;
    private Date createdate;
    private Batch batch;

    public TransactionQR() {

    }

    public TransactionQR(Long id, String codeqr, Date approvaltime, String merchant, String code_merchant, Long amount,
            Long saldoawal, Integer tracenumber, String state, Date createdate, Batch batch) {
        super();
        this.id = id;
        this.codeqr = codeqr;
        this.approvaltime = approvaltime;
        this.merchant = merchant;
        this.code_merchant = code_merchant;
        this.amount = amount;
        this.saldoawal = saldoawal;
        this.tracenumber = tracenumber;
        this.state = state;
        this.createdate = createdate;
        this.batch = batch;
    }

    public Long getId() {
        return id;
    }
    public Date getApprovalTime() {
        return approvaltime;
    }

    public Batch getBatch() {
        return batch;
    }

    public void setBatch(Batch batch) {
        this.batch = batch;
    }

    public void setApprovalTime(Date approvalTime) {
        this.approvaltime = approvalTime;
    }
    public void setId(Long id) {
        this.id = id;
    }

    public Date getApprovaltime() {
        return approvaltime;
    }

    public void setApprovaltime(Date approvaltime) {
        this.approvaltime = approvaltime;
    }

    public String getCodeqr() {
        return codeqr;
    }

    public void setCodeqr(String codeqr) {
        this.codeqr = codeqr;
    }



    public String getMerchant() {
        return merchant;
    }

    public void setMerchant(String merchant) {
        this.merchant = merchant;
    }

    public String getCode_merchant() {
        return code_merchant;
    }

    public void setCode_merchant(String code_merchant) {
        this.code_merchant = code_merchant;
    }

    public Long getAmount() {
        return amount;
    }

    public void setAmount(Long amount) {
        this.amount = amount;
    }

    public Long getSaldoawal() {
        return saldoawal;
    }

    public void setSaldoawal(Long saldoawal) {
        this.saldoawal = saldoawal;
    }

    public Integer getTracenumber() {
        return tracenumber;
    }

    public void setTracenumber(Integer tracenumber) {
        this.tracenumber = tracenumber;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public Date getCreatedate() {
        return createdate;
    }

    public void setCreatedate(Date createdate) {
        this.createdate = createdate;
    }   
}

我尝试使用Gson处理该列表:

Gson objGson = new GsonBuilder().setPrettyPrinting().create();
String json = objGson.toJson(QR.getAllTransaction());

System.out.println(json);

那样,它可以显示,但是不是来自POJO,我想和pojo一起将数据解析为json吗?

为什么我得到的错误无法转换为模型?有任何线索吗?

2 个答案:

答案 0 :(得分:1)

尝试将Select tr添加到getAllTransaction()的查询中

答案 1 :(得分:0)

QRTransactionDao和TransactionQR之间是什么关系?