• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Working with BLOB in Oracle and Java using hibernate

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic