-->
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: Retrieving byte[] with Hibernate3/Oracle 9i returns garbage
PostPosted: Thu Apr 21, 2005 2:30 am 
Beginner
Beginner

Joined: Wed Sep 24, 2003 3:14 am
Posts: 20
Location: Munich, Germany
Hi,

i have problems retrieving a blob from a Oracle 9i database, see the fragments of the mapping files and the relevant hibernate log below.
There are no exceptions, and inserting the blob in the database works fine, the BLOB content in the database is o.k..
The retrieving of the blob works fine, too, but unfortunately the blob content is garbage.

Bye,

Jürgen

Hibernate version: 3.0.1

Mapping documents:
Code:
<?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 package="data">
    <class name="AttachmentVO" table="attachments_new">
        <id name="attachmentId" column="attachment_id" >
            <generator class="sequence">
                <param name="sequence">attachment_id_seq</param>
            </generator>
        </id>

        <many-to-one name="message"
            column="message_id"         not-null="true"/>

        <property name="fileName"
            column="file_name"          not-null="true"/>

        <property name="fileData"       lazy="true"
            column="file_data"          not-null="true"/>

    </class>
</hibernate-mapping>


Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- DB connection -->
        <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">URL</property>
        <property name="connection.username">USER</property>
        <property name="connection.password">PASSWORD</property>
        <property name="jdbc.use_streams_for_binary">true</property>

        <!-- other Hibernate settings -->
        <property name="show_sql">true</property>

        <!-- mapping files -->
        <mapping resource="AttachmentVO.hbm.xml"/>

    </session-factory>
</hibernate-configuration>


Name and version of the database you are using:
Oracle 9i, Oracle 10g thin driver

Debug level Hibernate log excerpt:
[...]
Code:
08:00:54,834 [DEBUG] org.hibernate.loader.entity.EntityLoader: Static select for entity com.bmw.ehr.business.inbox.data.AttachmentVO: select attachment0_.attachment_id as attachment1_0_, attachment0_.message_id as message2_1_0_, attachment0_.file_name as file3_1_0_, attachment0_.file_data as file4_1_0_ from attachments_new attachment0_ where attachment0_.attachment_id=?
08:00:54,834 [DEBUG] org.hibernate.loader.entity.EntityLoader: Static select for entity com.bmw.ehr.business.inbox.data.AttachmentVO: select attachment0_.attachment_id as attachment1_0_, attachment0_.message_id as message2_1_0_, attachment0_.file_name as file3_1_0_, attachment0_.file_data as file4_1_0_ from attachments_new attachment0_ where attachment0_.attachment_id=?
08:00:54,844 [DEBUG] org.hibernate.loader.entity.EntityLoader: Static select for entity com.bmw.ehr.business.inbox.data.AttachmentVO: select attachment0_.attachment_id as attachment1_0_, attachment0_.message_id as message2_1_0_, attachment0_.file_name as file3_1_0_, attachment0_.file_data as file4_1_0_ from attachments_new attachment0_ where attachment0_.attachment_id=? for update
08:00:54,844 [DEBUG] org.hibernate.loader.entity.EntityLoader: Static select for entity com.bmw.ehr.business.inbox.data.AttachmentVO: select attachment0_.attachment_id as attachment1_0_, attachment0_.message_id as message2_1_0_, attachment0_.file_name as file3_1_0_, attachment0_.file_data as file4_1_0_ from attachments_new attachment0_ where attachment0_.attachment_id=? for update nowait

[...]
Code:
08:00:55,465 [DEBUG] org.hibernate.SQL: insert into attachments_new (message_id, file_name, file_data, attachment_id) values (?, ?, ?, ?)
Hibernate: insert into attachments_new (message_id, file_name, file_data, attachment_id) values (?, ?, ?, ?)
08:00:55,465 [DEBUG] org.hibernate.jdbc.AbstractBatcher: preparing statement
08:00:55,465 [DEBUG] org.hibernate.persister.entity.BasicEntityPersister: Dehydrating entity: [com.bmw.ehr.business.inbox.data.AttachmentVO#890]
08:00:55,465 [DEBUG] org.hibernate.type.LongType: binding '900' to parameter: 1
08:00:55,465 [DEBUG] org.hibernate.type.StringType: binding 'testname.txt' to parameter: 2
08:00:55,465 [DEBUG] org.hibernate.type.BinaryType: binding 'e4e1f4e1' to parameter: 3
08:00:55,465 [DEBUG] org.hibernate.type.LongType: binding '890' to parameter: 4
08:00:55,465 [DEBUG] org.hibernate.jdbc.AbstractBatcher: Adding to batch
08:00:55,465 [DEBUG] org.hibernate.jdbc.AbstractBatcher: Executing batch size: 1
08:00:55,465 [DEBUG] org.hibernate.jdbc.AbstractBatcher: success of batch update unknown: 0
08:00:55,465 [DEBUG] org.hibernate.jdbc.AbstractBatcher: about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
08:00:55,465 [DEBUG] org.hibernate.jdbc.AbstractBatcher: closing statement

[...]
Code:
08:00:55,535 [DEBUG] org.hibernate.loader.Loader: loading collection: [com.bmw.ehr.business.inbox.data.MessageVO.attachments#900]
08:00:55,535 [DEBUG] org.hibernate.jdbc.AbstractBatcher: about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
08:00:55,535 [DEBUG] org.hibernate.SQL: select attachment0_.message_id as message2___, attachment0_.attachment_id as attachment1___, attachment0_.attachment_id as attachment1_0_, attachment0_.message_id as message2_1_0_, attachment0_.file_name as file3_1_0_, attachment0_.file_data as file4_1_0_ from attachments_new attachment0_ where attachment0_.message_id=?
Hibernate: select attachment0_.message_id as message2___, attachment0_.attachment_id as attachment1___, attachment0_.attachment_id as attachment1_0_, attachment0_.message_id as message2_1_0_, attachment0_.file_name as file3_1_0_, attachment0_.file_data as file4_1_0_ from attachments_new attachment0_ where attachment0_.message_id=?
08:00:55,535 [DEBUG] org.hibernate.jdbc.AbstractBatcher: preparing statement
08:00:55,535 [DEBUG] org.hibernate.type.LongType: binding '900' to parameter: 1
08:00:55,575 [DEBUG] org.hibernate.jdbc.AbstractBatcher: about to open ResultSet (open ResultSets: 0, globally: 0)
08:00:55,575 [DEBUG] org.hibernate.loader.Loader: result set contains (possibly empty) collection: [com.bmw.ehr.business.inbox.data.MessageVO.attachments#900]
08:00:55,575 [DEBUG] org.hibernate.engine.CollectionLoadContext: uninitialized collection: initializing
08:00:55,575 [DEBUG] org.hibernate.loader.Loader: processing result set
08:00:55,575 [DEBUG] org.hibernate.loader.Loader: result set row: 0
08:00:55,575 [DEBUG] org.hibernate.type.LongType: returning '890' as column: attachment1_0_
08:00:55,575 [DEBUG] org.hibernate.loader.Loader: result row: EntityKey[com.bmw.ehr.business.inbox.data.AttachmentVO#890]
08:00:55,575 [DEBUG] org.hibernate.loader.Loader: Initializing object from ResultSet: [com.bmw.ehr.business.inbox.data.AttachmentVO#890]
08:00:55,575 [DEBUG] org.hibernate.persister.entity.BasicEntityPersister: Hydrating entity: [com.bmw.ehr.business.inbox.data.AttachmentVO#890]
08:00:55,575 [DEBUG] org.hibernate.type.LongType: returning '900' as column: message2_1_0_
08:00:55,575 [DEBUG] org.hibernate.type.StringType: returning 'testname.txt' as column: file3_1_0_
08:00:55,575 [DEBUG] org.hibernate.type.BinaryType: returning '80d48081818c808080818080808180808080d6398080a96a8080a9698084808480808080808080808080808080808080808080808b91f2be851c8080808080808080808080808080808080808080a969838080fa8080' as column: file4_1_0_
08:00:55,575 [DEBUG] org.hibernate.type.LongType: returning '900' as column: message2___
08:00:55,575 [DEBUG] org.hibernate.loader.Loader: found row of collection: [com.bmw.ehr.business.inbox.data.MessageVO.attachments#900]
08:00:55,575 [DEBUG] org.hibernate.engine.CollectionLoadContext: reading row
08:00:55,575 [DEBUG] org.hibernate.type.LongType: returning '890' as column: attachment1___
08:00:55,575 [DEBUG] org.hibernate.event.def.DefaultLoadEventListener: loading entity: [com.bmw.ehr.business.inbox.data.AttachmentVO#890]
08:00:55,575 [DEBUG] org.hibernate.event.def.DefaultLoadEventListener: entity found in session cache
08:00:55,575 [DEBUG] org.hibernate.event.def.DefaultLoadEventListener: attempting to resolve: [com.bmw.ehr.business.inbox.data.AttachmentVO#890]
08:00:55,575 [DEBUG] org.hibernate.event.def.DefaultLoadEventListener: resolved object in session cache: [com.bmw.ehr.business.inbox.data.AttachmentVO#890]
08:00:55,575 [DEBUG] org.hibernate.loader.Loader: done processing result set (1 rows)


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 3:22 am 
Beginner
Beginner

Joined: Mon Jan 31, 2005 7:53 am
Posts: 32
Location: Madrid - Spain
I also have the same problem. I'm also using the new Oracle JDBC driver 10g on my Oracle 9i database. I can insert blobs as byte[] perfectly but I can't retrieve its data. I always get an 86 bytes array of garbage.

Has anyone solve this problem?

I've read all blob-related posts of this forum and others. Besides, I've tried all my mind could think of with no success at all. :-(

Any help will be REALLY appreciated!! ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 9:06 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
It work fine for me.
I have this in mapping

<property name="image" type="blob" column="IMAGE" />

property in POJO is java.sql.Blob

regards


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 10:24 am 
Beginner
Beginner

Joined: Mon Jan 31, 2005 7:53 am
Posts: 32
Location: Madrid - Spain
Thank you very much, now IT WORKS!!!

My error was I was mapping blob as binary and then I put a column property with sql-type="blob".The right way is property type="blob".

I also wasn't using Hibernate.creteBlob method to create blobs and thus a big part of my problem.

Thank you to all!! ;-D


Top
 Profile  
 
 Post subject: seeing same problem w/Hibernate 3.0.5
PostPosted: Tue Jun 07, 2005 2:07 pm 
Newbie

Joined: Mon Mar 21, 2005 5:23 pm
Posts: 4
Location: Beaverton, OR
Musicolo, did you make this work using a byte[] as the Java type in your POJO or did you end up using java.sql.Blob? I can't find a solution for mapping a POJO byte[] <--> Oracle BLOB type using the 'binary' mapping. Has anyone figured this out?

HIBERNATE EXPERTS: I'm not having any trouble with the same Oracle BLOB mapping to byte[] w/Kodo JDO 3.3.2 so I know its not an Oracle issue. But we need to support both JDO and Hibernate with the same JAR or POJOs so I need to try to avoid hacking the POJO directly just to please Hibernate. What options would you pursue w/Hibernate 3.x if changing the POJO from byte[] to java.sql.Blob is not an option? Thanks. -Matt


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 08, 2005 3:31 am 
Beginner
Beginner

Joined: Mon Jan 31, 2005 7:53 am
Posts: 32
Location: Madrid - Spain
I got it using java.sql.Blob objects, although you can create a Blob from byte[] by using Hibernate.createBlob( Byte[] ) method.

If you still have any doubt, don't hesitate to asking me...

Good luck!! ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 4:42 am 
Newbie

Joined: Fri Jun 10, 2005 4:18 am
Posts: 3
Musicolo,

I have set my POJO using java.sql.Blob message, and have my hbm.xml like the following:
<property
name="message"
type="blob"
update="true"
insert="true"
access="property"
column="message"
not-null="false"
/>
I set my POJO message field using: setMessage(Hibernate.createBlob()).
my environment is:
hibernate.connection.driver_class = oracle.jdbc.driver.OracleDriver
hibernate.connection.url = jdbc:oracle:thin:@...
and i am using c3p0-0.9.0-pre6.jar connection pooling.
I always get error:
(util.JDBCExceptionReporter 57 ) SQL Error: 17410, SQLState: null
(util.JDBCExceptionReporter 58 ) No more data to read from socket
(def.AbstractFlushingEventListener 277 ) Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: could not insert:...

any idea?

rgds,

.satria.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 4:49 am 
Beginner
Beginner

Joined: Mon Jan 31, 2005 7:53 am
Posts: 32
Location: Madrid - Spain
Try something like that:

Code:
public void setImage (  byte[] buffer )
   {
       // Check parameter and create a BLOB with the given buffer
       if( buffer != null && buffer.lengh > 0)
       {
              this.image = Hibernate.createBlob( buffer );
       }
    }



Good luck! ;-)


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.