Hibernate – save image into database
To save an image into database, you need to define a table column as blob data type in MySQL, or equivalent binary type in others database. In Hibernate side, you can declare a byte array variable to store the image data.
Here’s an Maven project to use Hibernate to save an image into MySQL ‘avatar‘ table.
1. Table creation
An avatar table creation script in MySQL.
CREATE TABLE `mkyong`.`avatar` ( `AVATAR_ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `IMAGE` BLOB NOT NULL, PRIMARY KEY (`AVATAR_ID`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
2. Maven dependency
Add Hibernate and MySQL dependency.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.mkyong.common</groupId> <artifactId>HibernateExample</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>HibernateExample</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- MySQL database driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.9</version> </dependency> <!-- Hibernate framework --> <dependency> <groupId>hibernate</groupId> <artifactId>hibernate3</artifactId> <version>3.2.3.GA</version> </dependency> <!-- Hibernate library dependecy start --> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2</version> </dependency> <!-- Hibernate library dependecy end --> </dependencies> </project>
3. Avatar Model
Create a model class to store the avatar data. The image data type is array of bytes.
Avatar.java
package com.mkyong.common; public class Avatar implements java.io.Serializable { private Integer avatarId; private byte[] image; public Avatar() { } public Avatar(byte[] image) { this.image = image; } public Integer getAvatarId() { return this.avatarId; } public void setAvatarId(Integer avatarId) { this.avatarId = avatarId; } public byte[] getImage() { return this.image; } public void setImage(byte[] image) { this.image = image; } }
4. Mapping file
Create a Hibernate mapping file for avatar. The data type for image is binary.
Avatar.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.mkyong.common.Avatar" table="avatar" catalog="mkyong"> <id name="avatarId" type="java.lang.Integer"> <column name="AVATAR_ID" /> <generator class="identity" /> </id> <property name="image" type="binary"> <column name="IMAGE" not-null="true" /> </property> </class> </hibernate-mapping>
5. Hibernate configuration file
Hibernate configuration file to define the database connection and Hibernate mapping file.
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.bytecode.use_reflection_optimizer">false</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">password</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mkyong</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <mapping resource="com/mkyong/common/Avatar.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
6. Hibernate utility
A Hibernate utility class to get the database connection.
HibernateUtil.java
package com.mkyong.persistence; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml return new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } public static void shutdown() { // Close caches and connection pools getSessionFactory().close(); } }
7. Run it
Read a file “C:\\mavan-hibernate-image-mysql.gif” and save it into database, later get it from database and save it into another image file “C:\\test.gif“.
package com.mkyong.common; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.hibernate.Session; import com.mkyong.persistence.HibernateUtil; public class App { public static void main( String[] args ) { System.out.println("Hibernate save image into database"); Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); //save image into database File file = new File("C:\\mavan-hibernate-image-mysql.gif"); byte[] bFile = new byte[(int) file.length()]; try { FileInputStream fileInputStream = new FileInputStream(file); //convert file into array of bytes fileInputStream.read(bFile); fileInputStream.close(); } catch (Exception e) { e.printStackTrace(); } Avatar avatar = new Avatar(); avatar.setImage(bFile); session.save(avatar); //Get image from database Avatar avatar2 = (Avatar)session.get(Avatar.class, avatar.getAvatarId()); byte[] bAvatar = avatar2.getImage(); try{ FileOutputStream fos = new FileOutputStream("C:\\test.gif"); fos.write(bAvatar); fos.close(); }catch(Exception e){ e.printStackTrace(); } session.getTransaction().commit(); } }
Done.
Hello mkyong,
I am getting this error for second part of code.
If u can please assist me,
“java.io.FileNotFoundException: C:\test.jpg (Access is denied)”
even if file is present or file is not present.
thanks in advance
Hi mykong !!
very useful example..i m using the same codings with oracle 10 i data base..IDE-net beans, server-appace…but while i run the program..its throwing some errors ..its,
Nov 27, 2011 2:10:40 PM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 1461, SQLState: 72000
Nov 27, 2011 2:10:40 PM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: ORA-01461: can bind a LONG value only for insert into a LONG column
so dould u help me out from this problem..thanks in advance…
You try to insert a not “long” value into a table column which only accept “long” value, find out which field and fix it.
as for the mapping, could you give us the annotation way on this ??
and do i have to use those number 2 ?? Maven dependency ?? I don’t use Maven in my project
thanks
1. The annotation is rather simple to convert, please refer to Hibernate tutorials
2. You should use Maven
, nvm if you don’t, just grab the dependencies manually. How to get it? Visit the official website and download one by one, and remember choose the right version l! With Maven, everything is done for you.
fantastic example.. very simple and 100000% work !!
Hello,
Do u have an example where i can store and retrieve image from jsf page through database.
Thanks.
sir I want to upload image to database from any folder not just from c: using hibernate please help
if you know how to upload from “c:” driver, what other problem stopped you to upload from other folder ?
Hello Mkyong,
How are you ? Really useful program, once again i came here for your help, hey do you know how to save file to database from “web application” ? I tried a lot, but its only works if file size is <=2kb. Please, if you know then tell me how to do it.. when retrieving its fine, no problem with retrieving. But when i am going to save then its getting problem.. Its really helpful if its with struts2.. thanks a lot for your useful programs(I did blob and clob programs with standalone applcations).. i always came here to learn new things..
Thanking You..
Regards,
Arun.R.U.
“I tried a lot, but its only works if file size is <=2kb." This may configurable in your server or upload component limit, try study on it.
Hi,
Thanks for the information, did you tried uploading file in webapp ?
thanks…
Regards,
Arun.R.U
Struts
http://www.mkyong.com/struts/struts-file-upload-example/
Struts2
http://www.mkyong.com/struts2/struts-2-file-upload-example/
Spring MVC
http://www.mkyong.com/spring-mvc/spring-mvc-file-upload-example/
I am getting the Hibernate Exception
could not insert: [com.igate.image.SampleImage]
Post your caused by exception.
But what if the file and not just an image is beyond the maximum of heap memory ?
This code is running well and useful.
Image file should convert to byte first then save to database..
Thanks Mykong
I didn’t test it, but looks helpful. thx
This code is not working…The file is not uploading to dababase and here(Avatar avtar2 = (Avatar)sess.get(Avatar.class, avatar.getAvatarId());) we are getting error can ayone rectify it.
The code is well tested in my machine, may i have your error message?
useful example indeed..actually i am working on swt scribble application and i need to save the content i drawn(freehand )into a database using hibernate..how to achieve this using base64
[...] How to save an image into database A tutorial to show how to use Hibernate to save an image into database. [...]