It's not a secret anymore!*
The moose likes Other Application Frameworks and the fly likes Working with BLOB in Oracle and Java using hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Working with BLOB in Oracle and Java using hibernate" Watch "Working with BLOB in Oracle and Java using hibernate" New topic
Author

Working with BLOB in Oracle and Java using hibernate

Arpita Shah
Greenhorn

Joined: Aug 28, 2009
Posts: 15
Hello Friends.

This code snnipet may be useful in your application

JAVA Entity Class : ContactInfo which is having generated ID and Blob properties
JAVA POJO Class : Person : contains information of Person like first,middle and last name and its serializable as it is going to be stord in DB
hbm File : ContactInfo.hbm.xml

ContactInfo :package net.roseindia.tutorial.hibernate;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.sql.Blob;
import java.sql.SQLException;

import org.hibernate.Hibernate;

import antlr.collections.List;

public class ContactInfo implements Serializable
{
private long id;
private Blob familyMembers;
public Blob getFamilyMembers()
{
return familyMembers;
}

public void setFamilyMembers(Blob familyMembers)
{
this.familyMembers = familyMembers;
}

public long getId()
{
return id;
}

public void setId(long id)
{
this.id = id;
}


public byte[] toByteArray(Blob fromBlob)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try
{
return toByteArrayImpl(fromBlob, baos);
}
catch (SQLException e)
{
throw new RuntimeException(e);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
finally
{
if (baos != null)
{
try
{
baos.close();
}
catch (IOException ex)
{}
}
}
}

private byte[] toByteArrayImpl(Blob fromBlob, ByteArrayOutputStream baos) throws SQLException,
IOException
{
byte[] buf = new byte[4000];
InputStream is = fromBlob.getBinaryStream();
try
{
for (;;)
{
int dataSize = is.read(buf);
if (dataSize == -1)
break;
baos.write(buf, 0, dataSize);
}
}
finally
{
if (is != null)
{
try
{
is.close();
}
catch (IOException ex)
{}
}
}
return baos.toByteArray();
}
}

=========ContactInfo.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="net.roseindia.tutorial.hibernate.ContactInfo" table="CONTACT_INFO">
<id name="id" type="long" column="ID">
<generator class="assigned" />
</id>
<property name="temp" column="FAMILY_MEMBERS" type="blob"/>
</class>
</hibernate-mapping>


========Person Class =======
package net.roseindia.tutorial.hibernate;

import java.io.Serializable;

public class Person implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 8409613054478706163L;

private String firstName;

private String lastName;

private String middleName;

public String getFirstName()
{
return firstName;
}

public void setFirstName(String firstName)
{
this.firstName = firstName;
}

public String getLastName()
{
return lastName;
}

public void setLastName(String lastName)
{
this.lastName = lastName;
}

public String getMiddleName()
{
return middleName;
}

public void setMiddleName(String middleName)
{
this.middleName = middleName;
}
}


========Main Class Storing and retriving BLOB data ========

Session session = null;
Person p = new Person();
p.setFirstName("First");
p.setMiddleName("Middle");
p.setLastName("Last");

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(p);
oos.close();

byte[] personByteArray = baos.toByteArray();
ContactInfo info = new ContactInfo();
info.setId(1L);

Blob b = Hibernate.createBlob(personByteArray);
info.setFamilyMembers(b);
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(info);
tx.commit();

//Now Retriving Data

List<ContactInfo> contactInfos =session.createCriteria(ContactInfo.class).list();

for(int i=0; i<contactInfos.size();i++)
{
ContactInfo cInfo = contactInfos.get(i);
byte[] bretrived = cInfo.toByteArray(cInfo.getFamilyMembers());
ByteArrayInputStream bais = new ByteArrayInputStream(bretrived);
ObjectInputStream obj = new ObjectInputStream(bais);
Person retrivedPerson = (Person)obj.readObject();
System.out.println("retrived First Name :::: " + retrivedPerson.getFirstName());;
}

With Best Regards,
Arpita.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read.

You can edit your post by using the button.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Working with BLOB in Oracle and Java using hibernate
 
Similar Threads
Read/Write BLOB in byte[] with Hibernate in MySQL..
problem while reading multipart/form-data
Issue while retrieving BLOB from ORACLE 9i, using Hibernate 3.0
Inserting into table with Hibernate question
Deserialization issue if I have serialized object to ByteArrayOutputStream