views:

37

answers:

2

Hi,

I have the following:

@Entity
public class ExamplePhoto {

...

@Column(nullable= false)
private byte[] photo;

...
}

I am using Hibernate and a MySql database. I tried putting a mediumblob but got a "tinyblob expected error" message. Given the fact that the photo could be of up to 400KB the tinyblob won't do the job. How can I map this field? Is this the most elegant way of handling images?

Thanks in advance!

A: 

Did you try to add the @Lob annotation to the photo field?

@Column(nullable=false)
@Lob
private byte[] photo;

And to answer your second part, whether it is elegant to handle images that way: Depends on how frequently you are serving them. Database access is usually more expensive than plain file access.

Willi
Thanks for your answer! I tried it but with the @Lob annotation it expects a "longblob", not a medium blob
spderosso
What about `@Column(nullable=false, columnDefinition="mediumblob")`?
Willi
A: 

Given the fact that the photo could be of up to 400KB the tinyblob won't do the job. How can I map this field?

IIRC, Hibernate also uses the length attribute of the Column annotation when mapping a property as Lob. Try the following:

@Lob @Column(nullable= false, length=400000)
private byte[] photo;

Is this the most elegant way of handling images?

As others have stated, the (better) alternative would be to store them on the file system and to write the path in the database.

Pascal Thivent