-->
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.  [ 5 posts ] 
Author Message
 Post subject: mapping byte[] to blob in MySQL
PostPosted: Fri Nov 10, 2006 12:09 am 
Newbie

Joined: Tue May 30, 2006 12:24 am
Posts: 6
Location: Sydney, Australia
Hibernate version: 3.1.3
Database type and version: MySQL 5.0

Hi, all!
I'm trying to store byte[] in POJO as an SQL type of blob (in MySQL).
I have the following mapping:
Code:
<property name="thumbnail" type="binary"/>

and my java object has:
Code:
byte[] thumbnail;

This creates tinyblob in MySQL, but I want to have a type of blob so I can store an image which size is bigger than what tinyblob can handle.
I tried the following as well.
Code:
<property name="thumbnail" type="blob"/>

While this did create the column with blob type, it did not allow my java object to interact with database (which is understandable i guess since the Hibernate type blob was meant to be used with Blob java type...)

My current work around is that I use binary to let hibernate create the database schema with 'tinyblob' type, then, once the schema is created, i manually change the column type to blob. Hibernate didn't change the column type back to tinyblob. So, it works.

I guess I could implement a customer type, but it seems over-doing it for a simple case like this, and I'm not too confident in implementing a customer type myself.

My question is, is there an easier way to map this byte[] to blob without implementing a customer type?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 12, 2006 5:53 pm 
Newbie

Joined: Tue May 30, 2006 12:24 am
Posts: 6
Location: Sydney, Australia
mmm.. the last line of my posting was meant to say, is there an easier way to map byte[] to blob without implementing a custom type?

(just noticed a typo...)


Top
 Profile  
 
 Post subject: blob stuff
PostPosted: Sun Nov 12, 2006 8:07 pm 
Beginner
Beginner

Joined: Tue Feb 08, 2005 1:29 pm
Posts: 20
Hi,

I m using blob without any problem.

Here is what I ve done:
public class My extends Object implements IOfferBookMarketOverview {
private byte[] description;

public String getDescription() {
return new String(description);
}

public void setDescription(String s) {
description = s.getBytes();
}
}

<hibernate-mapping package="com.etil.sudetp.business.manager.offerbook.impl" >
...
<property name="description" type="binary" column="DESCRIPTION" access="field" />
...
</hibernate-mapping>

CREATE TABLE `offermarketinfo` (
...
`DESCRIPTION` blob NOT NULL default '',
...
)

Hope that helps... but as I said, never had any problem up to now.

Cheers.
DvJ


Top
 Profile  
 
 Post subject: blob and byte[]
PostPosted: Sun Nov 12, 2006 8:48 pm 
Newbie

Joined: Tue May 30, 2006 12:24 am
Posts: 6
Location: Sydney, Australia
Hmm...
That did not work for me.
Differences with my object and mapping against yours was that my object had getter and setter that accepts and returns byte[] instead of converting it from/to String. And I didn't use access="field" in mapping file.
But I tried with your configuration, yet, did not create blob column, but it created tinyblob still. What database and version are you using? as stated above, i'm using MySQL 5.0 with hibernate 3.1.3

I'm going to try again with hibernate 3.2...


Top
 Profile  
 
 Post subject: byte[] to tinyblob, blob, mediumblob, or longblob
PostPosted: Sun Nov 12, 2006 10:54 pm 
Newbie

Joined: Tue May 30, 2006 12:24 am
Posts: 6
Location: Sydney, Australia
Found a solution. very simple...
all I had to do was, in my mapping, specify the length... for example,
Code:
<property name="thumbnail" type="binary" length="50000"/>


Can't believe I didn't see this before...


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