[英]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.