How to insert image into MySQL using Hibernate?

1

I'm using java with hibernate and would like to know how to insert an image into the mysql database.

I have several information being persisted with hibernate, just missing the image.

    
asked by anonymous 22.11.2014 / 16:43

1 answer

2
Assuming you have a "blob" image column in a table, you need to define the data type in the template class as a byte array:

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 for the image:

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

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

For more information, there is a tutorial here: link

    
22.11.2014 / 23:49