How to insert image in Mysql using Hibernate?

Asked

Viewed 894 times

1

I am using java with Hibernate and would like to know how to insert an image in the mysql database.

I have several information being persisted with Hibernate, missing only the image.

1 answer

2


Assuming you have a "Image" column of type blob in a table, you will need to define the data type in the model class as an array of bytes (byte[]):

private byte[] imagem;

...

public byte[] getImagem() {
    return this.imagem;
}

public void setImagem(byte[] imagem) {
    this.imagem = imagem;
}

And in the mapping file add the property referring to the image:

<property name="imagem" type="binary">
    <column name="imagem" />
</property>

When saving to the database, you programmatically use the setImage method.

For more information, there is a tutorial (in English) here: http://www.mkyong.com/hibernate/hibernate-save-image-into-database/

  • Thank you for your help William.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.