-->
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.  [ 7 posts ] 
Author Message
 Post subject: How to map a byte[] attribute in hibernate3.1
PostPosted: Sun Oct 15, 2006 2:34 am 
Newbie

Joined: Sun Oct 15, 2006 1:52 am
Posts: 5
I am new to Hibernate.
I am trying to use hibernate3.1 with MySql5.0.

I have a Java POJO class with a 'byte[] attriubte' and I need to write/read that to my Database.

Which approach should I use:
1. http://www.hibernate.org/73.html. I tried that but 'BinaryBlobType' does not compile due to assemble() method is not implemented

2. This blog posting here:
http://hansonchar.blogspot.com/2005/06/ ... te-in.html

It mention it is for Oracle. But i am using Mysql 5.0.

3. BlobUtils in BeanLib:
http://beanlib.sourceforge.net/3.2.7/api/

Thanks for any help.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 15, 2006 4:41 pm 
Beginner
Beginner

Joined: Tue Sep 26, 2006 11:46 pm
Posts: 33
You could use the binary type for the property:

http://www.hibernate.org/hib_docs/v3/re ... ping-types

Though I'm not sure what database type this will map to in MySQL.

If it doesn't map to blob, and you need to use blob, then use that Blob user type you linked to.

assemble and disassemble should be able to be implemented easily by just returning the first parameter from each method.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 15, 2006 5:25 pm 
Newbie

Joined: Sun Oct 15, 2006 8:07 am
Posts: 4
You should map it with @Lob. That should create pointer field in database which points to binary stream. Than you use that pointer as java.sql.Blob, and with that you can get input stream, and use Hibernate.createBlob for output stream.

However, if length of your byte[] array isn't big, you could user @Column with columnDefinition to map to specific database type.

I cannot give you example for MySQL, but in PostgreSQL is something like:
Code:
@Lob
Blob bytes;


or
Code:
@Lob
@Column(columnDefinition="bytea")
byte[] bytes;
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 15, 2006 7:05 pm 
Newbie

Joined: Sun Oct 15, 2006 1:52 am
Posts: 5
Thanks. The size of the byte[] can be as big as 400K. So can I still use the suggestion that you provided?

And in your PostgreSQL example, how can I do that not using hibernate Annotations?

Code:

@Lob
@Column(columnDefinition="bytea")
byte[] bytes;


and where should I put this:

Code:
@Lob
Blob bytes;


Thank you.[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 16, 2006 8:51 am 
Newbie

Joined: Sun Oct 15, 2006 8:07 am
Posts: 4
Well, if you have files large as much as 400K you should use Blob.

I can’t help you with xml mapping, as I don’t use that kind of mapping, but I'm sure you can find corresponding xml tag for @Lob.


Best regards..


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 29, 2006 12:40 am 
Newbie

Joined: Wed Sep 20, 2006 10:15 pm
Posts: 12
Quote:
I cannot give you example for MySQL, but in PostgreSQL is something like:
Code:
@Lob
Blob bytes;


or
Code:
@Lob
@Column(columnDefinition="bytea")
byte[] bytes;
[/code]


I tried this but got a weird error message. It seems the generated insert statement tried to insert an integer. Here's a snippet of the log:

Code:
17:34:26,373 ERROR JDBCExceptionReporter:72 - Batch entry 0 insert into Photo (dogID, image, id) values (2, 16891, 3) was aborted.  Call getNextException to see the cause.
17:34:26,373  WARN JDBCExceptionReporter:71 - SQL Error: 0, SQLState: 42804
17:34:26,373 ERROR JDBCExceptionReporter:72 - ERROR: column "image" is of type bytea but expression is of type integer
17:34:26,383 ERROR AbstractFlushingEventListener:301 - Could not synchronize database state with session
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update


Here's the column mapping in the Photo class:

Code:
   @Lob
   @Column(columnDefinition="bytea")
   private byte[] image;
   public byte[] getImage(){return image;}
   public void setImage(byte[] image){
      this.image = image;
   }


Have anyone encoutered this before? Or perhaps successfully stored an image into a PostgreSQL database?

Thanks,
Dany.


Top
 Profile  
 
 Post subject: Re:
PostPosted: Thu Jul 01, 2010 6:34 pm 
Beginner
Beginner

Joined: Mon Jun 28, 2010 1:02 pm
Posts: 21
danywu wrote:
Here's the column mapping in the Photo class:

Code:
   @Lob
   @Column(columnDefinition="bytea")
   private byte[] image;
   public byte[] getImage(){return image;}
   public void setImage(byte[] image){
      this.image = image;
   }


Have anyone encoutered this before? Or perhaps successfully stored an image into a PostgreSQL database?

Thanks,
Dany.


You are using MySQL as far as I could understand. I think "bytea" SQL type is PostgreSQL specific. MYSQL, PosgreSQL and other RDBMSes implement BLOBs differently. Try to specify only @Lob annotation. In case you use PostgreSQL you don't want to go with bytea anyway, the other method is better: in that case the PostgreSQL will use an OID reference and store the data in a different table automatically. If you use byte[] type in Java, Hibernate should map this to the OID type BLOB automatically with PostgreSQL, but I still have problems:
https://forum.hibernate.org/viewtopic.php?f=1&t=1005584


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