-->
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.  [ 3 posts ] 
Author Message
 Post subject: Blob and Hibernate
PostPosted: Thu Aug 25, 2005 11:17 am 
Beginner
Beginner

Joined: Fri Jan 07, 2005 2:47 pm
Posts: 45
Salut,

J'ai utilisé le code de Hanson que l'on peut trouver à cette addresse (http://hansonchar.blogspot.com/2005/06/ ... te-in.html). Toutefois j'obtien l'erreur suivante soit un update après mon insert.

Pouvez-vous m'aider?????

Merci

Hibernate version:
Hibernate 2.1.x

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping
package="MyPackage"
default-cascade="save-update">

<class name="DigitalFile" table="DIGITAL" >
<id name="id"
column= "FILE_NAME"
type="java.lang.String"
length="10">
<generator class="assigned"/>
</id>
<!-- NOTE: Timestamp needed because assigned identifier.when using
TIMESTAP tag,ensure date field is not instatiated in the class -->
<timestamp name="dateChanged"
column="CHANGED_ON"
unsaved-value="null"/>

<!--TIMESTAMP values-->
<property name="lastModified"
type="timestamp"
column="LAST_MODIFIED"
update="false"/>

<!--Regular ATTRIBUTE values-->


<property name="filesize"
type="java.lang.Long"
column="FILE_SIZE"
update="true"
insert="true"
not-null="false"/>

<property name="filecontentBlob"
type="blob"
column="FILE_CONTENTS"/>


Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:
[25/08/05 10:31:19:205 EDT] 68ac0279 SystemOut O Hibernate: update DIGITAL set CHANGED_ON=?, FILE_SIZE=?, FILE_CONTENTS=?, CHANGED_BY=? where FILE_NAME=? and CHANGED_ON=?
[25/08/05 10:31:19:355 EDT] 68ac0279 StaleObjectSt W net.sf.hibernate.StaleObjectStateException An operation failed due to stale data
[25/08/05 10:31:19:395 EDT] 68ac0279 StaleObjectSt W net.sf.hibernate.StaleObjectStateException TRAS0014I: The following exception was logged net.sf.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) for SAP.BusinessObjects.DigitalFile instance with identifier: 0000000093.pdf
Name and version of the database you are using:
Oracle 9.x
The generated SQL (show_sql=true):
true
Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 31, 2005 11:59 am 
Beginner
Beginner

Joined: Tue Jul 05, 2005 4:44 am
Posts: 40
Location: Paris, France
Salut,

Voulant utilisé les blob avec Oracle 9.2, j'ai eu le même type d'erreur.
Entre temps, j'ai récupéré les nouveaux driver Oracle 10 (qui rapellons-le, sont compatibles avec Oracle 9.2) et ça marche nickel sans même passer par un UserType. (de Blob à String)

Si ça peut t'aider !!

_________________
Fred


Top
 Profile  
 
 Post subject: Blob and Hibernate
PostPosted: Wed Aug 31, 2005 12:10 pm 
Beginner
Beginner

Joined: Fri Jan 07, 2005 2:47 pm
Posts: 45
Merci pour ta réponse toutefois il ne m'était pas possible d'updater le driver d'Oracle.

Voici la solution qui a été trouvé et modifié par plusieurs developpeurs et qui fonctionne avec hibernate 2.x et Oracle driver 9.x si ça peut aider quelqu'un dans le futur.

Code à copier et coller au bon endroit. :::)))

Définir une classe de type usertype

* Purpose: The BinaryBlobType will convert a blob into
* a byte array and back again.
*
// */

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.sql.Blob;

import java.io.OutputStream;
import java.io.IOException;

import oracle.sql.BLOB;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.UserType;

public class BinaryBlobType implements UserType
{
/**
* @see net.sf.hibernate.UserType#sqlTypes()
*/
public int[] sqlTypes()
{
return new int[] { Types.BLOB };
}

/**
* @see net.sf.hibernate.UserType#returnedClass()
*/
public Class returnedClass()
{
return byte[].class;
}

/**
* @see net.sf.hibernate.UserType#equals(Object, Object)
*/
public boolean equals(Object x, Object y)
{
return (x == y)
|| (x != null
&& y != null
&& java.util.Arrays.equals((byte[]) x, (byte[]) y));
}

/**
* @see net.sf.hibernate.UserType#nullSafeGet(ResultSet, String[], Object)
*/
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException
{
Blob blob = rs.getBlob(names[0]);
return blob.getBytes(1, (int) blob.length());
}

/**
* @see net.sf.hibernate.UserType#nullSafeSet(PreparedStatement, Object, int)
*/
public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException
{
oracle.sql.BLOB blob = oracle.sql.BLOB.createTemporary(st.getConnection(),false,BLOB.DURATION_SESSION);
blob.open(BLOB.MODE_READWRITE);

OutputStream out = blob.getBinaryOutputStream();

try
{
out.write((byte[]) value);
out.flush();
out.close();
}
catch (IOException e)
{
throw new SQLException("failed write to blob" + e.getMessage());
}

blob.close();
((oracle.jdbc.OraclePreparedStatement)(st)).setBLOB(index,blob);
}

/**
* @see net.sf.hibernate.UserType#deepCopy(Object)
*/
public Object deepCopy(Object value)
{
if (value == null) return null;

byte[] bytes = (byte[]) value;
byte[] result = new byte[bytes.length];
System.arraycopy(bytes, 0, result, 0, bytes.length);

return result;
}

/**
* @see net.sf.hibernate.UserType#isMutable()
*/
public boolean isMutable()
{
return true;
}

}

Voici le mapping a mettre dans le fichier xml

<property name="filecontent"
type="Package.Utility.BinaryBlobType"
column="FILE_CONTENTS"/>

Et dans l'objet, voici la déclaration ainsi que le getter et setter pour hibernate.

private byte[] filecontent;

/**
* Returns the filecontent.
* @return byte[]
*/
public byte[] getFilecontent() {
return filecontent;
}

/**
* Sets the filecontent.
* @param filecontent The filecontent to set
*/
public void setFilecontent(byte[] filecontent) {
this.filecontent = filecontent;
}


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