-->
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: Insert Blob using SQLQuery
PostPosted: Mon Mar 15, 2010 11:33 am 
Newbie

Joined: Mon Mar 15, 2010 11:11 am
Posts: 2
I need to get a row from one table and insert the same row into another table. Everything works fine except for inserting blobs. I am getting the following error.

Quote:
insert into eproduct values (420681515,426875589,'','G-TEL 100','','','',0,0,0,1,0,0,1,0,0,'Bla Bla Bla Bla Bla Bla Bla',org.hibernate.lob.SerializableBlob@6a086a,org.hibernate.lob.SerializableBlob@14e8936)

2010-03-15 17:21:19,871 ERROR [org.hibernate.util.JDBCExceptionReporter] - <ORA-01729: database link name expected
>


If I insert the last to values (which are blobs) as null the insert works fine.


Code:

   public Product insertProduct(Product product) {

      Session session = getSession();
      
      String sql="insert into " + PRODUCT +" values (" + product.getId() + "," +
      product.getProductType().getId() + ",'"  + convertStringNulls(product.getProductCode())+ "','"
      + convertStringNulls(product.getProductName())+ "','" + convertStringNulls(product.getModelName()) + "','"
      + convertStringNulls(product.getProductURL())+ "','"
      + convertStringNulls(product.getSapMaterialCode())+ "',"
      + convertBooleanValue(product.isBusiness())+ "," + convertBooleanValue(product.isDealerWeb())+ ","
      + convertBooleanValue(product.isDiscontinued())+ "," + convertBooleanValue(product.isFullSupport())+ "," +
      convertBooleanValue(product.isLimitedSupport())+ ","
      + convertBooleanValue (product.isOutOfStock())+ ","
      + convertBooleanValue(product.isResidential())+","+ convertBooleanValue(product.isSpecialOffer())+","
      + convertBooleanValue (product.isTrolley())+ ",'"
      + convertStringNulls(product.getSummaryDescription())+"',"
      + product.getImage() + "," + product.getSummaryImage() + ")";
      
      try {        
          SQLQuery query = session.createSQLQuery(sql);        
          query.executeUpdate();
       } catch (HibernateException he) {
          throw he;
       } finally {
          session.close();
       } 
       
       return product;
   }



The blob fields above (product.getImage() and product.getSummaryImage()) are of type Blob. Anybody got a solution?


Top
 Profile  
 
 Post subject: Re: Insert Blob using SQLQuery
PostPosted: Mon Mar 15, 2010 5:16 pm 
Regular
Regular

Joined: Thu May 07, 2009 5:56 am
Posts: 94
Location: Toulouse, France
try to use a parameterized query rather than string approach

String sql="insert into " + PRODUCT +" values (?, ?, ...,?)"

SQLQuery query = session.createSQLQuery(sql);

query.setBinary(int position, byte[] val) //for blob
query.setBoolean(int position, boolean val) //no need convertBooleanValue()
....
position - the position of the parameter in the query string, numbered from 0.

take a look at https://www.hibernate.org/hib_docs/v3/a ... Query.html

_________________
everything should be made as simple as possible, but not simpler (AE)


Top
 Profile  
 
 Post subject: Re: Insert Blob using SQLQuery
PostPosted: Tue Mar 16, 2010 4:24 am 
Newbie

Joined: Mon Mar 15, 2010 11:11 am
Posts: 2
hallmit wrote:
try to use a parameterized query rather than string approach

String sql="insert into " + PRODUCT +" values (?, ?, ...,?)"

SQLQuery query = session.createSQLQuery(sql);

query.setBinary(int position, byte[] val) //for blob
query.setBoolean(int position, boolean val) //no need convertBooleanValue()
....
position - the position of the parameter in the query string, numbered from 0.

take a look at https://www.hibernate.org/hib_docs/v3/a ... Query.html


Thank you. Everything is working.


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.