-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Blob Image retrieval problem in Hibernate
PostPosted: Thu Nov 17, 2011 2:56 am 
Newbie

Joined: Thu Nov 17, 2011 2:05 am
Posts: 3
I have a thick client application in which I want to save an image into DB. I am sending an the as a byte[]

byte[] array = ImageClass.getImageBytes(imagePath);

the byte[] is converted to a SerialBlob in setAttributes methode in POJO

my bean class

public class School {
private int schoolId;
private String schoolName;
private Blob logo;
private String schoolType;

public School(){
}

public Integer getSchoolId() {
return this.schoolId;
}

public void setSchoolId(Integer schoolId) {
this.schoolId = schoolId;
}

public String getSchoolName() {
return this.schoolName;
}

public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}

public Blob getLogo() {
return this.logo;
}

public void setLogo(Blob logo) {
this.logo = logo;
}


public String getSchoolType() {
return this.schoolType;
}

public void setschoolType(String schoolType) {
this.schoolType = schoolType;
}


public void setAttributes(HashMap dto){

schoolName = (String)dto.get(DTOFactory.School.SCHOOL_NAME);
schoolType = (String)dto.get(DTOFactory.School.TYPE);
logo = new SerialBlob((byte[])dto.get(DTOFactory.School.LOGO));

}

public HashMap getAttributes(){
HashMap dto = DTOFactory.getSchoolDTO();
dto.put(DTOFactory.School.SCHOOL_NAME, getSchoolName());
dto.put(DTOFactory.School.LOGO, getLogo());
dto.put(DTOFactory.School.TYPE, getSchoolType());

return dto;
}
}

And my mapping file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class entity-name="school" name="erp.domain.School" table="efe_school">
<id column="SCHOOL_ID" name="schoolId" type="int">
<generator class="native"/>
</id>

<property name="schoolName">
<column name="SCHOOL_NAME"/>
</property>


<property name="logo" type="blob">
<column name="LOGO"/>
</property>

<property name="schoolType">
<column name="SCHOOL_TYPE"/>
</property>

</class>
</hibernate-mapping>


then its saved in my dao class

@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public void create(HashMap dto) {
School school = new School();
school.setAttributes(dto);
sessionFactory.getCurrentSession().save("school",school);
}



All these are working fine and the image is saved in DB

And retrives the data in Dao class like


public HashMap load() {
School school = (School)sessionFactory.getCurrentSession().createQuery("from edp.domain.School").uniqueResult();
return school.getAttributes();

}

the data is reached at the alient side and I trying to put the Image back in a JLabel

Blob image = ((Blob)response.get(LOGO));
byte[] array = ImageClass.getImageBytes(image);
ImageIcon imageIcon = new ImageIcon(array);
schoolImageLabel.setIcon(imageIcon);

ImageClass is used to retrievebytes from the blob,


ImageClass.getImageBytes() methode looks like

public static byte[] getImageBytes(Blob blob){
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
InputStream in = ((Blob)blob).getBinaryStream();

int n = 0;
while ((n=in.read(buf))>=0)
{
baos.write(buf, 0, n);
}
in.close();
byte[] bytes = baos.toByteArray();
return bytes;
} catch(Exception e) {
System.err.println("blob to byte[] : " + e);
}
return null;
}


Here what i am getting is an exception :

"java.lang.IllegalStateException:Blogs may not be accessed after serialization"

My doubt is that is it not get serialized at the time of retrieval from DB.

also when I tried to convert to serialBlob at client side

SerialBlob image = ((SerialBlob)response.get(LOGO));
byte[] array = ImageClass.getImageBytes(image);
ImageIcon imageIcon = new ImageIcon(array);
schoolImageLabel.setIcon(imageIcon);

an exception got like : org.hibernate.lob.SerializableBlob cannot be converted to javax.sql.rowset.serial.SerialBlob


Please help I am stuck with this for a long time. Any help will be greatly appreciated. Thanks in advance


Top
 Profile  
 
 Post subject: Re: Blob Image retrieval problem in Hibernate
PostPosted: Fri Nov 18, 2011 6:16 am 
Newbie

Joined: Thu Nov 17, 2011 2:05 am
Posts: 3
and this is my error

ERROR org.hibernate.property.BasicPropertyAccessor - IllegalArgumentException in class: erp.domain.School, setter method of property: logo
ERROR org.hibernate.property.BasicPropertyAccessor - expected type: [B, actual value: org.hibernate.lob.SerializableBlob


Top
 Profile  
 
 Post subject: Re: Blob Image retrieval problem in Hibernate
PostPosted: Sat Dec 03, 2011 11:26 pm 
Newbie

Joined: Sat Dec 03, 2011 2:38 pm
Posts: 3
Were you able to resolve the issue? If so can you please post the same..I am facing the same issue and unable to resolve it.

http://forum.springsource.org/showthrea ... izableBlob

Posted thread in forums also.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.