1. Create the table (e.g. create.sql)
2. Configure a user to access the table (e.g. user = imageuser, pass = imagepass)
3. Compile src/hibernate.cfg.xml, src/example/Image.java, src/example/Image.hbm.xml and src/example/Main.java.
4. Make sure you've got an image located at C:\image1.jpg.
5. Run Main.class.
You'll need these dependencies to compile the code:
- hibernate 3
- jakarta commons io
You'll need these dependencies to run the code:
- hibernate 3
- jakarta commons io
- mysql driver
--- create.sql ---
Code:
create table image (id bigint not null, content mediumblob, primary key (id));
--- src/hibernate.cfg.xml ---
Code:
<?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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/test</property>
<property name="connection.username">imageuser</property>
<property name="connection.password">imagepass</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<mapping resource="example/Image.hbm.xml"/>
</session-factory>
</hibernate-configuration>
--- src/example/Image.java ---
Code:
package example;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.io.IOUtils;
public class Image {
private Long id;
private byte[] content = new byte[0];
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public byte[] getContent() {
return content;
}
public void setContent(byte[] content) {
this.content = content;
}
public void readContentFrom(InputStream in) throws IOException {
setContent(IOUtils.toByteArray(in));
}
public void writeContentTo(OutputStream out) throws IOException {
IOUtils.write(getContent(), out);
}
public void readContentFrom(File file) throws IOException {
FileInputStream fis = new FileInputStream(file);
try {
readContentFrom(fis);
} finally {
fis.close();
}
}
public void writeContentTo(File file) throws IOException {
FileOutputStream fos = new FileOutputStream(file);
try {
writeContentTo(fos);
} finally {
fos.close();
}
}
}
--- src/example/Image.hbm.xml ---
Code:
<?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="example.Image" table="image">
<id name="id">
<generator class="increment" />
</id>
<property name="content" type="binary" length="65536"/>
</class>
</hibernate-mapping>
--- src/example/Main.java ---
Code:
package example;
import java.io.File;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Main {
public static void main(String[] args) throws Exception {
Configuration configuration = new Configuration();
configuration.configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
try {
Transaction transaction = session.beginTransaction();
try {
Image image = new Image();
image.readContentFrom(new File("C:\\image1.jpg"));
session.save(image);
} catch (Exception ex) {
transaction.rollback();
throw ex;
}
transaction.commit();
} finally {
session.close();
}
}
}