【问题标题】:Ljava.lang.Object; cannot be cast to modelLjava.lang.Object;不能转换为模型
【发布时间】:2018-07-13 09:17:11
【问题描述】:

在这种情况下,我想将 Json 显示到 java hibernate 中的响应页面,来自 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?

为什么我得到错误 can't cast to model ?有什么线索吗?

【问题讨论】:

标签: java hibernate pojo


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    QRTransactionDao 和 TransactionQR 的关系是什么?

    【讨论】:

    • QRTransactionDao 是查询类似方法的代码,然后 TransactionQR 是 servlet,我调用 Dao 到 servlet 以在 json 中显示数据
    猜你喜欢
    • 2013-12-27
    • 2019-04-26
    • 2012-05-01
    • 2013-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    相关资源
    最近更新 更多