简体   繁体   English

如何使用休眠从数据库中获取Blob值?

[英]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. 我正在开发一个桌面应用程序,它使用Hibernate连接mySql数据库,在该数据库中,该表没有主键。 so am getting the pojo class has two java file. 所以我得到的pojo类有两个java文件。 The pojo class is, pojo课是

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 // 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. 在检索值时,从obj[1]获取值,但是在obj[0]获取的对象不是值。 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. 在Hibernate中, fieldvalue列表示为byte[] ,而在检索值时我直接打印obj[0]因此我将字节数组作为值。 To get a value, need to convert that obj[0] to string. 要获取值,需要将该obj[0]转换为字符串。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Java从MySql数据库中检索Blob pdf文件 - How to retrive Blob pdf file from MySql database using java 如何使用CriteriaBuilder从数据库的现有表中检索数据,而不在休眠中使用pojo类? - How to retrive data from an existing table in database using CriteriaBuilder but without using pojo classes in hibernate? 如何使用 HIbernate 作为 BLOB 将此图像保存到 MySQL 数据库? - How to save this image to MySQL database using HIbernate as BLOB? 如何通过使用Hashmap中的键来检索值 <String, String> ? - How to retrive Value by using key from Hashmap<String, String>? 如何使用 java 从 Oracle 检索 CLOB 值 - how to Retrive the CLOB value from Oracle using java 如何使用Hibernate检查值并从数据库中检索它(如果存在)? - How to check the value and retrieve it from database if it exists using Hibernate? 如何从数据库中检索图像并将其显示在div上 - how to retrive image and display it on div from database 使用 hibernate 从数据库中检索 blob 并使用 jsp 在网页上显示 - Retrieving blob from database using hibernate and displaying on web page using jsp Vert.x使用BLOB和hibernate对来自数据库的数据进行了分块回答 - Vert.x chunked answer with data from database using BLOB and hibernate Hibernate - Retrive没有Blob字节数组[]的文件列表 - Hibernate - Retrive List of files without Blob byte array[]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM