开发者

Mapping a byte[] that can grow up to 400KB on MySql

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 cou开发者_开发技巧ld 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!


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜