简体   繁体   中英

show all list query java hibernate

i have this function to query in hibernate:

public List<TransactionQR> getAllTransaction() throws HibernateException {

        return this.session.createQuery("SELECT id FROM TransactionQR").list();
    }

then is success to show the data like this in html:

[2, 3]

but when i add more column in SELECT like this:

public List<TransactionQR> getAllTransaction() throws HibernateException {

            return this.session.createQuery("SELECT id, codeqr FROM TransactionQR").list();
        }

the result show like this:

[Ljava.lang.Object;@25026824, [Ljava.lang.Object;@170b75f9]

what is Ljava.lang.Object;@25026824 ? is return object, can i handle it to convert from list to json ? i have model TransactionQR.java :

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

}

the result is i want to show all data from database in list

In second case, since you are selecting two attributes, that is why session.createQuery("").list returns a list of object array( List<Object[]> ) . At each index of list you will find an object array. Each array will have two indexes. First index will provide id while the second one would provide codeqr. So, basically you need to iterate over the list. Then fetch each value individually like arr[0], arr[1]..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM