简体   繁体   中英

How to retrive blob value from database using hibernate?

I am developing one desktop application which i used Hibernate to connect mySql database, In that database the table doesn't have the primary key. so am getting the pojo class has two java file. The pojo class is,

package pojo;
public class Entries implements java.io.Serializable {

    private EntriesId id;

    public Entries() {
    }

    public Entries(EntriesId id) {
        this.id = id;
    }

    public EntriesId getId() {
        return this.id;
    }

    public void setId(EntriesId id) {
        this.id = id;
    }

}

//POJO2

package pojo;

import java.util.Arrays;
import java.util.Date;

public class EntriesId implements java.io.Serializable {

    private String userid;
    private String name;
    private Integer ver;
    private String bexval;
    private String boidval;
    private Integer empid;
    private String fieldname;
    private byte[] fieldvalue;
    private Date lstdatetime;

    public EntriesId() {
    }

    public EntriesId(String userid, String name, Integer ver, String bexval,
            String boidval, Integer empid, String fieldname, byte[] fieldvalue,
            Date lstdatetime) {
        this.userid = userid;
        this.name = name;
        this.ver = ver;
        this.bexval = bexval;
        this.boidval = boidval;
        this.empid = empid;
        this.fieldname = fieldname;
        this.fieldvalue = fieldvalue;
        this.lstdatetime = lstdatetime;
    }}

Am going to retrieve the value from the database,

Session session=null;
    Transaction tx=null;
    try{
        session=HibernateUtil.getSessionFactory().openSession();
        tx=session.beginTransaction();
        Query q=session.createQuery("select a.id.fieldvalue,count(a.id.fieldvalue) from Entries as a where a.id.lstdatetime between '2015-08-01' and '2015-08-24' and a.id.fieldname='*NAME' group by a.id.fieldvalue");
        List<Object[]> list=(List<Object[]>) q.list();
        System.out.println("List size :"+list.size());
        for(Object[] obj:list)
        {
            System.out.println(obj[0]);
            System.out.println(obj[1]);

        }
        tx.commit();
        session.close();
    }catch(Exception e)
    {
        System.out.println("Exception found here :"+e);
    }

While retrieve the the value am getting the value from obj[1] but in obj[0] am getting the object not a value. How can i get the raw data? Any one help me to retrieve the value.

In Hibernate the fieldvalue column is represented as byte[] , while retrieving the value i directly print the obj[0] so i get the byte array as value. To get a value, need to convert that obj[0] to string.

for(Object[] obj:list)
{
    byte ptext[] = (byte[])obj[0];
    String value = new String(ptext);
    System.out.println(obj[1]+" : "+value);
}

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