-->
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.  [ 8 posts ] 
Author Message
 Post subject: Oracle Long.
PostPosted: Mon May 24, 2004 12:48 am 
Newbie

Joined: Mon May 24, 2004 12:36 am
Posts: 4
Hello
How do I handle an Oracle8.x Long data type column ? I tried getting the object to work by setting a java.sql.Clob field against the long data type column which works fine while inserting data but I cant retreive the same when I do a find/get. I end up with an INVALID COLUMN TYPE SQL EXCEPTION. In brief here is my code.

Code:
<property name="theLong" type="clob" column="DESCRIPTION" />


/** nullable persistent field */
private Clob theLong;

Persistant obj = (Persistant) session.get(Persistant.class, id, LockMode.UPGRADE);
// This is where the error is thrown.



I do not get the error thrown while I insert but fails only when I try to retrieve the object. Any ideas how to implement this. Please !
FYI : I read through http://www.hibernate.org/56.html .

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 24, 2004 10:26 am 
Newbie

Joined: Mon May 24, 2004 12:36 am
Posts: 4
Anybody, Somebody Please. Ideas on handling Oracle Long Datatype in Hibernate.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 24, 2004 1:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
I had luck mapping Oracle LONG RAW columns to both String and String[] using Serializable. That should hold true for LONG also.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 24, 2004 2:33 pm 
Newbie

Joined: Mon May 24, 2004 12:36 am
Posts: 4
Thanks for the feedback Steve. I got the mapping to work with String as well but it fails when the length of the String exceeds 4K. This was the reason why I had to try making the property in the persistent class a Clob which apparently fails when I try to retreive the data back. Any more ideas ?

Thanks Galore.


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 24, 2004 2:37 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Did you make the java type String and then explicitly map the type in hbm.xml as Serializable? That's what worked for me, not just using the hibernate type="string"...


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 24, 2004 3:08 pm 
Newbie

Joined: Mon May 24, 2004 12:36 am
Posts: 4
Okay I made the change but now it throws a new error. I hope this is what you want me to do. I think it happens only when the data exceeds 4K. The finder also fails now.

Code:
<property name="theLong" type="java.io.Serializable" column="DESCRIPTION" />


/** nullable persistent field */
private String theLong;

Persistant obj = (Persistant) session.get(Persistant.class, id, LockMode.UPGRADE);
// This is where the error is thrown.



The error that gets thrown is

Code:
java.sql.SQLException: ORA-01461: can bind a LONG value only for insert into a LONG column

   at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:168)
   at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:208)
   at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:543)
   at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1405)
   at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteFetch(TTC7Protocol.java:822)
   at oracle.jdbc.driver.OracleStatement.executeNonQuery(OracleStatement.java:1446)
   at oracle.jdbc.driver.OracleStatement.doExecuteOther(OracleStatement.java:1371)
   at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1900)
   at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:363)
   at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:22)
   at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:468)
   at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:442)
   at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:29)
   at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2407)
   at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2360)
   at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2229)
   at net.sf.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:61)


Top
 Profile  
 
 Post subject: Try this custom type out
PostPosted: Thu May 27, 2004 1:46 pm 
Newbie

Joined: Tue Dec 09, 2003 4:45 pm
Posts: 14
I think I found code for a custom type that handles Oracle LONG columns in one of the hibernate wiki pages. I'd post the linke instead of this, but I can't remember where I found it. Create a class like this:

Code:
package yourpackage;

import net.sf.hibernate.*;
import java.sql.*;
import java.io.*;

public class LongType
  implements UserType {
  public LongType() {
  }

  public int[] sqlTypes() {
    return new int[] {
      java.sql.Types.LONGVARCHAR};
  }

  public Class returnedClass() {
    return java.lang.String.class;
  }

  public boolean equals(Object x, Object y) throws HibernateException {
    return (x == y)
      || (x != null
          && y != null
          && (x.equals(y)));
  }

  public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws
    HibernateException, SQLException {
    Object out = "";
    try {
      InputStream stream = rs.getAsciiStream(names[0]);
      byte[] buff = new byte[1024];
      int len = 0;
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      while ( (len = stream.read(buff, 0, 1024)) > -1) {
        bos.write(buff, 0, len);
      }
      out = bos.toString();
    }
    catch (IOException ex) {
      //worth printing b/c this means a failure occurred reading from stream
      ex.printStackTrace();
    }
    catch (Throwable t) {
      //eat this exception b/c it is of no interest (i.e. stream is null for null attribute value)
    }
    return out;
  }

  public void nullSafeSet(PreparedStatement st, Object value, int index) throws
    HibernateException, SQLException {

    try {
      String s = (String)value;
      if (s == null)
        s = "";
      InputStream is = new ByteArrayInputStream(s.getBytes());
      st.setAsciiStream(index, is, s.length());
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public Object deepCopy(Object value) throws HibernateException {
    if (value == null) {
      return null;
    }
    return new String( (String) value);
  }

  public boolean isMutable() {
    return false;
  }

}


In your mapping file, use type="yourpackage.LongType" and you should be good to go.

_________________
Mike Davison


Top
 Profile  
 
 Post subject: Small correction
PostPosted: Thu May 27, 2004 1:48 pm 
Newbie

Joined: Tue Dec 09, 2003 4:45 pm
Posts: 14
Just note that that code I posted only prints stack traces and doesn't do anything with them. The set method also puts an empty space into the column when the mapped attribute is null. Sorry...

_________________
Mike Davison


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.