-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: binary images and mysql
PostPosted: Tue Feb 20, 2007 9:49 am 
Newbie

Joined: Tue Feb 20, 2007 9:45 am
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.02

I would like to be able to take .jpg files and insert them into mysql. I have the book "Java Persistance with Hibernate Book" and could not find enough information to get rolling with. I googled for some example and came up with some Oracle Hibernate issues, but nothing to get me rolling with mysql and blobs.

I was hoping someone on this list may know of some links, or be able to explain how this works.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 10:24 am 
Newbie

Joined: Tue Feb 20, 2007 10:19 am
Posts: 4
Do you want help with the Hibernate part or the MySQL part or both?

What version of MySQL are you using?


Top
 Profile  
 
 Post subject: Hibernate Part
PostPosted: Tue Feb 20, 2007 10:50 am 
Newbie

Joined: Tue Feb 20, 2007 9:45 am
Posts: 3
The Hibernate part to me is what matters. I would like to know how a blob should be defined and if there are certain settings such as size limitations, etc. that may come into play.

As far as mysql, it should just take the data. Seems that is out of play, if the hbm definition is correct and the pojo to send the data to said hbm.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 10:51 am 
Newbie

Joined: Tue Feb 20, 2007 9:45 am
Posts: 3
PS, version of mysql 5.0


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 11:07 am 
Newbie

Joined: Mon Oct 10, 2005 2:36 pm
Posts: 2
With JDK1.5 and higher you can use a SerialBlob to hold the binary data and simply persist that with Hibernate.

Previous to that you would have to have the row where the blob is to go already created, select from that row and use the resulting blob object to write your data into.

HTH,
W


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 20, 2007 11:56 am 
Newbie

Joined: Tue Feb 20, 2007 10:19 am
Posts: 4
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();
        }
    }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.