-->
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.  [ 9 posts ] 
Author Message
 Post subject: persist java.awt.Image using Hibernate
PostPosted: Sat Sep 17, 2005 6:35 am 
Newbie

Joined: Wed Sep 14, 2005 5:13 pm
Posts: 13
Hibernate version:
3.0.5

Name and version of the database you are using:
MySQL

hey guys!
i would like to store images to a database using Hibernate.

can you give me any clues on how to do that?
is java.awt.Image suitable or should i use anything else?

thanks a lot in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 17, 2005 7:15 am 
Senior
Senior

Joined: Thu Aug 04, 2005 4:54 am
Posts: 153
Location: Birmingham, UK
Use a BLOB would be the most common method. Or store the file on the file system and use a string as a reference to the file. When wondering a question like that, think this "how would the data be stored in the db?", if you can't think of a variable type then you probably can't do it.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 17, 2005 8:25 am 
Newbie

Joined: Wed Sep 14, 2005 5:13 pm
Posts: 13
thanks for your answer!

i tried the "blob" approach, but my main problem lies in how the
image should be serialized at all.
java.awt.image is does not implement "serializable", so what's the way to tell hibernate how to serialize the object to the database?

i get the following exception when i try to store the object:

----

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sun.awt.image.ToolkitImage
at org.hibernate.type.SerializableType.toBytes(SerializableType.java:74)
at org.hibernate.type.SerializableType.deepCopyNotNull(SerializableType.java:70)
at org.hibernate.type.MutableType.deepCopy(MutableType.java:25)
at org.hibernate.type.TypeFactory.deepCopy(TypeFactory.java:290)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:227)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:160)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:108)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:96)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:468)
at org.hibernate.engine.Cascades$5.cascade(Cascades.java:154)
at org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:771)
at org.hibernate.engine.Cascades.cascade(Cascades.java:720)
at org.hibernate.engine.Cascades.cascadeCollection(Cascades.java:895)
at org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:792)
at org.hibernate.engine.Cascades.cascade(Cascades.java:720)
at org.hibernate.engine.Cascades.cascade(Cascades.java:847)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:363)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:265)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:160)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:108)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:96)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:468)
at org.hibernate.engine.Cascades$5.cascade(Cascades.java:154)
at org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:771)
at org.hibernate.engine.Cascades.cascade(Cascades.java:720)
at org.hibernate.engine.Cascades.cascadeCollection(Cascades.java:895)
at org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:792)
at org.hibernate.engine.Cascades.cascade(Cascades.java:720)
at org.hibernate.engine.Cascades.cascade(Cascades.java:847)
at org.hibernate.event.def.AbstractFlushingEventListener.cascadeOnFlush(AbstractFlushingEventListener.java:121)
at org.hibernate.event.def.AbstractFlushingEventListener.prepareEntityFlushes(AbstractFlushingEventListener.java:112)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:59)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at net.bitalchemy.omnitheca.persistence.StorageTools.save(StorageTools.java:19)
at .....


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 17, 2005 12:49 pm 
Senior
Senior

Joined: Thu Aug 04, 2005 4:54 am
Posts: 153
Location: Birmingham, UK
Then you're main problem is to work out how to convert java.awt.image to a blob. Logic would dictate that at some point the image was in a format that could be read i.e. JPEG, so you need to find your orignal image and convert it to a byte stream.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 6:51 am 
Newbie

Joined: Tue Sep 27, 2005 8:12 am
Posts: 12
These sample methods may give some idea about conversion to byte[] and vice-versa.

private byte[] getImage(String imagePath)
{
try
{
//obtain a writer
Iterator writers = ImageIO.getImageWritersByFormatName("png");
ImageWriter writer = (ImageWriter)writers.next();

//set destination for writer
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
writer.setOutput(ios);

//get the image e.g. "C:\\Work\\eclipseworkspace\\TestProject\\icons\\RedRectangle.png"
File file = new File(imagePath);
BufferedImage bi = ImageIO.read(file);

//write the image
writer.write(bi);

//convert to byte array
return baos.toByteArray();
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}

private Image getImage(byte[] bytes)
{
try
{
//set a byte stream reader
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ImageInputStream iis = ImageIO.createImageInputStream(bais);

//convert to image
BufferedImage bi = ImageIO.read(iis);

return bi;
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 4:57 am 
Newbie

Joined: Wed Sep 14, 2005 5:13 pm
Posts: 13
thanks ndzpac!
that's very helpful.

i wonder if i should write a custom serializer for java.awt.Image
or just have my persistable class return a byte array in
"getImage".

first approach would be:
class MyClass {
private Image img;
public Image getImage() {
//return the Image object
}
//....
}
like this, the class is very clean and easy to use, but i have to write
some custom serializer and tell hibernate it should use that custom
serializer for the "img" property in my class.
(how to do that???)


second approach:
class MyClass {
private Image img;
public byte[] getImage() {
//return the byte array of the Image
}
//....
}


do you have experience with custom serializers in hibernate?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 6:48 am 
Newbie

Joined: Tue Sep 27, 2005 8:12 am
Posts: 12
I don’t think that you have to do some custom serialization to store image in database.

If you want to store an image, just you have to do is

POJO:

class MyClass
{
private Object image;
.....


MAPPING file:
In hibernate mapping file set type to binary or blob for this field

<property
name="image"
column="IMAGE"
type="binary"



So when you want to store:

MyClass obj = new MyClass ();
byte[] imageBytes = getImage("somepathforimage");
obj.setImage(imageBytes);
.....
session.save(obj);

And after retrieving the object from database you can convert into image object:

Image image = getImage((byte[]) obj.getImage());


HTH


Top
 Profile  
 
 Post subject: blob size problem
PostPosted: Tue Oct 25, 2005 1:04 pm 
Newbie

Joined: Wed Sep 14, 2005 5:13 pm
Posts: 13
i got it working!
can now serialize a byte[] to mysql!

next problem:
in my mapping file i have
<property name="media" column="media_blob" type="binary"/>

which hibernate translates into a "blob" column for mysql.
sadly, a mysql "blob" can only hold 255 bytes.
i need a "medium_blob" which would be 16MB.

how do i tell hibernate to use a bigger blob variant?

thanks in advance


Top
 Profile  
 
 Post subject: found the solution
PostPosted: Tue Oct 25, 2005 4:10 pm 
Newbie

Joined: Wed Sep 14, 2005 5:13 pm
Posts: 13
ok, i found it out by myself.
for anyone who gets to that thread in the future:
set the "length" attribute of the property to sth.
that requires a bigger type of blob.

for mysql 4.1 eg, when you say 'type="java.sql.Blob"'
in the mapping file, you will get a "blob" in the database, which
is 65 k.
if you want sth. bigger do
<property name="media" column="media_blob" type="java.sql.Blob" length="65538"/>

and you will get a "mediumblob" in the DB which can hold 16 MB.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.