-->
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.  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Creating a BLOB in Oracle
PostPosted: Fri Oct 10, 2003 12:16 pm 
Newbie

Joined: Thu Sep 04, 2003 9:25 am
Posts: 1
I'm trying to use hibernate to read / write blobs in a Oracle9 database.

The mappings is very simple:

Code:
<class
    name="TBlobs"
    table="TBLOBS"
    dynamic-update="true">

...
    <property
        name="data"
        type="blob"
        column="DATA"/>

...


The corresponding java class looks like
Code:
public class TBlobs implements Serializable {

...

   private Blob fData;

   public Blob getData() {
      return fData;
   }

   public void setData(Blob data) {
      fData = data;
   }

...



Nothing fancy in the hibernate configuration (I tried with and without hibernate.jdbc.use_streams_for_binary ) with the following parameters:
Code:
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:...
        <property name="dialect">net.sf.hibernate.dialect.Oracle9Dialect</property>


My code (to insert a blob) looks like :

Code:
...
      Session sess = factory.openSession();
      Transaction t = sess.beginTransaction();
      TBlobs b = new TBlobs();
      Blob blob = Hibernate.createBlob(stream);
      b.setData(blob);
      sess.save(b);
      t.commit();


At runtime, I got the following error:

2969 [main] WARN util.JDBCExceptionReporter - SQL Error: 17090, SQLState: null
2969 [main] ERROR util.JDBCExceptionReporter - operation not allowed: streams type cannot be used in batching
2984 [main] ERROR util.JDBCExceptionReporter - Could not synchronize database state with session

The SQL generated looks like:
Hibernate: insert into TBLOBS (NAME, DATA, ID) values (?, ?, ?)


What am I doing wrong here?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2003 11:46 am 
Beginner
Beginner

Joined: Wed Oct 08, 2003 6:34 am
Posts: 29
I have the same problem, do you have any solution for this?

Regards
Jose


Top
 Profile  
 
 Post subject: Same Problem
PostPosted: Mon Dec 01, 2003 6:07 pm 
Newbie

Joined: Wed Nov 05, 2003 11:35 pm
Posts: 1
I also have the same problem. Did anybody find a solution?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 01, 2003 7:33 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Try disabling JDBC batch updates.


Top
 Profile  
 
 Post subject: i have the same question too!
PostPosted: Sun Apr 25, 2004 5:40 am 
Regular
Regular

Joined: Tue Dec 30, 2003 2:35 am
Posts: 85
disabling JDBC batch updates

but how can i disable the batch of updates!!!!

thanks a lot!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 25, 2004 2:59 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
read the doc.
hibernate.jdbc.batch_size=0

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 4:19 am 
Newbie

Joined: Sun Mar 14, 2004 1:43 am
Posts: 2
emmanuel wrote:
read the doc.
hibernate.jdbc.batch_size=0

that's not work


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 21, 2004 2:03 pm 
Newbie

Joined: Mon Aug 02, 2004 12:11 pm
Posts: 10
Location: Mexico D.F.
You put <property name="hibernate.jdbc.batch_size">0</property> in the hibernate.cfg.xml file and it's work!, but i have a doubt if oracle really save this data type, because when i call the method getBlob() for example, it's give me null .


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 21, 2004 2:25 pm 
Newbie

Joined: Fri Aug 27, 2004 5:36 pm
Posts: 16
Location: Pittsburgh
read this

http://www.hibernate.org/56.html


It works !


Top
 Profile  
 
 Post subject: Problem with BLOB
PostPosted: Thu Oct 28, 2004 2:38 pm 
Newbie

Joined: Mon Aug 02, 2004 12:11 pm
Posts: 10
Location: Mexico D.F.
gavin wrote:
Try disabling JDBC batch updates.


Hi Gavin, i so sorry with a problem and my intention is not to clear your time, but i have some days and i can not resolve this problem.

The message that hibernate rise is this :

hibernate java.sql.SQLException: Data size bigger than max size for this type: 4101120

part of de code is this :

ArchivosCampaniaValue objCampValue2 = (ArchivosCampaniaValue) iter.next();
TmkCampaniaArchivo objCampArch = new TmkCampaniaArchivo();
TmkCampaniaArchivoPK objCampArchPK = new TmkCampaniaArchivoPK();
objCampArchPK.setFlCampania(objCampania.getFlCampania());
objCampArchPK.setNbArchivo(objCampValue2.getNombreArchivoCampania());
objCampArch.setId(objCampArchPK);
objCampArch.setClTipoArchivo(new Integer(objCampValue2.getTipoArchivo()));
objCampArch.setArchivo(Hibernate.createBlob(data));
objCampArch.setClUsrModifico(objCampania.getClUsrModifico());
objCampArch.setFeUltmod(new Date());
//crear el elemento de tipo campa


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 29, 2004 11:59 am 
Regular
Regular

Joined: Tue Sep 28, 2004 6:34 pm
Posts: 50
did you try:
http://www.hibernate.org/56.html

L.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 29, 2004 1:00 pm 
Newbie

Joined: Mon Feb 09, 2004 11:14 am
Posts: 16
We had great difficulty getting this to work. Below are is some of our code we use to insert a BLOB into Oracle. Hope this helps... I am hoping someone posts a more elegant solution.
if (HibernateAbstractSessionUtil.isOracleDB()){
//To bypass a bug (in the oracle driver) when the blob size is more that 4k,
//first insert a single byte blob, then
//get a output stream to that blob and write the actual data
//this also needs the special insertBlob method in the dao that upgrades the LockMode
stagingTransaction.setTransactionDetail(Hibernate.createBlob(new byte[1]));

dao.insertBlob(stagingTransaction);

//Couldn't use oracle.sql.BLOB when invoked with in the container (ClassCastException) so use
//the oracle blob from Weblogic 8.1 Oracle extensions
//TODO: refactor vendor specific code (Weblogic/Oracle) to a seperate package
weblogic.jdbc.vendor.oracle.OracleThinBlob blob = (weblogic.jdbc.vendor.oracle.OracleThinBlob) stagingTransaction.getTransactionDetail();
OutputStream os = blob.getBinaryOutputStream();
os.write(bos.toByteArray());
os.flush();
os.close();
} else {
stagingTransaction.setTransactionDetail(Hibernate.createBlob(bos.toByteArray()));
dao.insertBlob(stagingTransaction);
}


public void insertBlob(Persistent persistent) throws PersistenceException {
try {
Session session = HibernateAbstractSessionUtil.getCurrentSession();
doInsert(persistent, session);
session.flush();
session.refresh(persistent, LockMode.UPGRADE); //grabs an Oracle BLOB
} catch (PropertyValueException pve) {
logger.error(ErrorCodes.SP00146_INVALID_MISSING_NULL_DATA, pve,
getClassPropertyName(pve.getPersistentClass(), pve.getPropertyName()));
throw new PersistenceException();
} catch (Exception e) {
logger.error(ErrorCodes.SP00068_PERSISTENCE_ERROR, e, "Exception occurred inserting persistent object : " + persistent);
throw new PersistenceException();
}

}


Top
 Profile  
 
 Post subject: Solution
PostPosted: Mon Mar 24, 2008 9:02 am 
Newbie

Joined: Mon Mar 24, 2008 8:46 am
Posts: 1
I just replaced the oracle 9.2 driver with jbdc oracle 10 driver and the problem was solved.


Top
 Profile  
 
 Post subject: Re: Creating a BLOB in Oracle
PostPosted: Tue Oct 13, 2009 10:38 am 
Newbie

Joined: Thu Sep 24, 2009 11:49 am
Posts: 8
USE THE FOLLOWING WAY, IT WILL WORK
-----------------------------------------

<?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="contact.Picture" table="PICTURE">
<id name="id" type="integer" column="ID" >
<generator class="assigned"/>
</id>
<property name="image" column="IMAGE" type="java.sql.Blob"/>
</class>
</hibernate-mapping>


-------------------------------------------


package contact;

import java.util.*;
import java.sql.Blob;
public class Picture{

private int id;
private Blob image;


public Blob getImage() {
return this.image;
}
public void setImage(Blob image) {
this.image = image;
}


/**
* @return ID Returns ID
*/
public int getId() {
return id;
}

/**
* @param l Sets the ID
*/
public void setId(int l) {
id = l;
}


}

----------------------------------------
package contact;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Transaction;
import org.hibernate.Hibernate;
import java.io.*;
import java.util.*;
import java.sql.Blob;


public class First_Insert_Blob {
public static void main(String[] args) {
Session session = null;
FileInputStream fs=null;
File file=null;

try{
file = new File("D:\\xyz.gif");
fs = new FileInputStream(file);
Blob photo=Hibernate.createBlob(fs);
SessionFactory sessionFactory = new
Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
Transaction tx = session.beginTransaction();
System.out.println("Inserting Record");
Picture picture = new Picture();
picture.setId(2);
picture.setImage(photo);
session.save(picture);
tx.commit();
System.out.println("Done");
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
// Actual contact insertion will happen at this step
session.flush();
session.close();

}

}
}


Top
 Profile  
 
 Post subject: Re: Creating a BLOB in Oracle
PostPosted: Fri Feb 26, 2010 11:37 am 
Newbie

Joined: Wed Nov 25, 2009 11:35 am
Posts: 17
Follow code works for me:

Code:
public void nullSafeSet(PreparedStatement st, Object value, int index)  throws HibernateException, SQLException {
     ...
      CallableStatement cs = st.getConnection().prepareCall("{ call dbms_lob.createTemporary(?, TRUE) }");
      cs.registerOutParameter(1, java.sql.Types.CLOB);
      cs.execute();
      java.sql.Clob c = cs.getClob(1);
      ((oracle.sql.CLOB) c).putString(1, (String)value);
      st.setClob(index, c);
     ...

- appears to be independent on what kind of proxy is involved.

Hopefuly someone will find it useful.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 16 posts ]  Go to page 1, 2  Next

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.