-->
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.  [ 10 posts ] 
Author Message
 Post subject: problem while storing blob in db2
PostPosted: Tue Aug 16, 2005 6:07 am 
Newbie

Joined: Sat Mar 19, 2005 5:31 am
Posts: 5
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:2.0

Mapping documents:n/a

Code between sessionFactory.openSession() and session.close():n/a

Name and version of the database you are using:db2 8.1

Debug level Hibernate log excerpt:n/a


Can any one get me the sample program to store and retrieve blob datatype in db2 database. I tried with oracle its working fine. But when i tried with db2, i am not even able get session from the session factory.



with regards,


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 6:32 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
So show you exceptions!

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 9:24 am 
Newbie

Joined: Sat Mar 19, 2005 5:31 am
Posts: 5
hi Emanuel,

thanks for ur response


now i able to get the session a object.

can u get me some sample example to store and retrieve blob datatype value in db2 database.....


[color = red]In case of oracle:[/color]

The code i used is:


SessionFactory sf =new Configuration().configure().buildSessionFactory();
Session session = null;
Transaction tx = null;

session=sf.openSession();
tx = session.beginTransaction();
ai.setAttachBlob(Hibernate.createBlob(new String(" ").getBytes()));
session.save(ai);
tx.commit();
session.close();

session=sf.openSession();
tx = session.beginTransaction();
AttachmentInfo attachmentInfo = (AttachmentInfo)session.load(AttachmentInfo.class,new Integer(ai.getId()),LockMode.UPGRADE);
BLOB blob = (BLOB) attachmentInfo.getAttachBlob();
OutputStream os = blob.getBinaryOutputStream();
os.write(attachObject);
os.close();
tx.commit();
session.close();


when i use the same code for db2....

BLOB blob = (BLOB) attachmentInfo.getAttachBlob();
OutputStream os = blob.getBinaryOutputStream();

blob.getBinaryOutputStream();

is not possible thats for oracle

how do i write? or get me some code or some url to get the information

with regard,

viswa





[/u]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 9:34 am 
Senior
Senior

Joined: Thu Aug 04, 2005 4:54 am
Posts: 153
Location: Birmingham, UK
Please post your hibernate.cfg.xml file, the mappings for the relevant object, the stack trace etc.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 10:29 am 
Newbie

Joined: Sat Mar 19, 2005 5:31 am
Posts: 5
hi emmanuel,


I have added db2jcc.jar,license.jar in the class path


[color = red]This is hibernate.cfg.xml[/color]

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

<property name="connection.driver_class">com.ibm.db2.jcc.DB2Driver</property>
<property name="connection.url">jdbc:db2://wstzion:50000/wsd_m1_x</property>
<property name="connection.username">db2admin</property>
<property name="connection.password">db2admin</property>
<property name="show_sql">true</property>
<property name="dialect">net.sf.hibernate.dialect.OracleDialect</property>


<mapping resource="Attachments.hbm"/>


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


[color = red]This is Attachments.hbm[/color]

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

<hibernate-mapping>

<class
name="AttachmentInfo"
table="test_attachments"
>

<id
name="id"
type="int"
column="id"
>
<generator class="increment" />
</id>

<property
name="attachBlob"
type="blob"
column="attach_blob"
/>
</class>
</hibernate-mapping>


[color = red]This is AttachmentInfo.java[/color]



import java.io.Serializable;
import java.sql.Blob;



public class AttachmentInfo implements Serializable{

private int id;
private Blob attachBlob = null;

public AttachmentInfo(){

} //for Hibernate by Baskar


/**
* @return Returns the attachBlob.
*/
public Blob getAttachBlob() {
return attachBlob;
}
/**
* @param attachBlob The attachBlob to set.
*/
public void setAttachBlob(Blob attachBlob) {
this.attachBlob = attachBlob;
}
/**
* @return Returns the id.
*/
public int getId() {
return id;
}
/**
* @param id The id to set.
*/
public void setId(int id) {
this.id = id;
}


}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 10:39 am 
Senior
Senior

Joined: Thu Aug 04, 2005 4:54 am
Posts: 153
Location: Birmingham, UK
Code:
<property name="dialect">net.sf.hibernate.dialect.OracleDialect</property>


Your SQL dialect is still set to Oracle dialect not DB2 dialect. Change it to net.sf.hibernate.dialect.DB2Dialect


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 10:51 am 
Newbie

Joined: Sat Mar 19, 2005 5:31 am
Posts: 5
sorry emmanuel,

i have now changed oracle dialect to db2 dialect... then how could i store and retrieve....


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 11:10 am 
Senior
Senior

Joined: Thu Aug 04, 2005 4:54 am
Posts: 153
Location: Birmingham, UK
Before we get to that I think there's another issue that needs to be covered. As your dialect was set incorrectly the table generate by hbm2ddl may be incorrect. If you do use auto ddl generation you need to regenerate the DB2 tables.

Then it should be something along the lines of

Code:
PersistedObject po(PersistedObject)session.load(PersistedObject.class, 1)
OutputStream os = po.getBLOB.getOutputStream()


Where PersistedObject would have an attribute of type BLOB[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 11:26 am 
Newbie

Joined: Sat Mar 19, 2005 5:31 am
Posts: 5
hai,

the hint that u have given:

PersistedObject po=(PersistedObject)session.load(PersistedObject.class, 1)
OutputStream os = po.getBLOB.getOutputStream() ;



when i tried

AttachmentInfo attachmentInfo = (AttachmentInfo)session.load(AttachmentInfo.class,new Integer(ai.getId()),LockMode.UPGRADE);
OutputStream os = attachmentInfo.getAttachBlob.getOutputStream();


here

attachmentInfo.getAttachBlob.getOutputStream();

i am getting method getOutputStream() is undefined for the type Blob

please tell me wat to do next...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 4:03 am 
Senior
Senior

Joined: Thu Aug 04, 2005 4:54 am
Posts: 153
Location: Birmingham, UK
Well the best thing to do when this happens is to stop trusting the random man on the internet and double check the methods of Blob.

After I did this I noticed that BLOB actually has the method getBinaryStream() [Retrieves the BLOB designated by this Blob instance as a stream.]

So you were lead off track by a strange man who doesn't check things he thought he remembered but you also forgot that you can't believe everything.


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