-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problem with Hibernate.createBlob() in 3.5-Final
PostPosted: Thu May 06, 2010 5:26 pm 
Newbie

Joined: Tue Dec 15, 2009 4:56 pm
Posts: 2
Hi, all!
I am currently worked on an project that saves binary files to DB (PostgreSQL 8.3, Hibernate 3.5).

I have next code:

Domain object:
Code:
@Entity
@Table(name="ITEM")
public class Item {

   @Id @GeneratedValue
   @Column(name="ITEM_ID")
   private Long id = null;
   
   @Lob
   @Column(name="BLOB_LOCATOR")
   private Blob blobLocator;
   
   public Item() {}
   
   public Long getId() {
      return id;
   }
      
   public Blob getBlobLocator() {
      return blobLocator;
   }
   
   public void setBlobLocator(Blob blobLocator) {
      this.blobLocator = blobLocator;
   }
   
}


Snippet from DAO class:
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();

session.beginTransaction();
Item item = new Item();
item.setBlobLocator(Hibernate.createBlob(new byte[100], session));
session.saveOrUpdate(item);
session.getTransaction().commit();


When i try run it, throws exception:
Code:
Exception in thread "main" java.lang.ClassCastException: $Proxy9 cannot be cast to org.hibernate.engine.jdbc.LobCreationContext
   at org.hibernate.Hibernate.getLobCreator(Hibernate.java:420)
   at org.hibernate.Hibernate.getLobCreator(Hibernate.java:416)
   at org.hibernate.Hibernate.createBlob(Hibernate.java:412)


If instead createBlob(byte[], Session) used createBlob(byte[]), it's work fine! But method createBlob(byte[]) deprecated in Hibernate 3.5.
When instead HibernateUtil.getSessionFactory().openSession() i use HibernateUtil.getSessionFactory().getCurrentSession(), that's work fine.

Everybody know how to solve this problem? Thank you for you help!


Top
 Profile  
 
 Post subject: Re: Problem with Hibernate.createBlob() in 3.5-Final
PostPosted: Thu Sep 02, 2010 12:07 pm 
Regular
Regular

Joined: Sun Feb 14, 2010 3:29 pm
Posts: 58
Location: USA
I see the same issue as well.

The deprecated method still works though. The Hibernate.createBlob(blobBytes, session) fails for session that uses ThreadLocalSessionContext. It doesn't seem like the session wrapped ThreadLocalSessionContext (which produced by sessionFactory.getCurrentSession()) has a way to let you unwrap it, which is needed by the by Hibernate.createBlob(blobBytes, session). Now if we work with JPA, it works because EnityManager.unwrap(Session.class) gives you back a real session object. Obviously if you use sessionFactory.openSession() you won't have the problem, but then it's not a context managed session. :(

I think this is Hibernate's bug and they should fix this.

The current solution to continue use a ThreadLocalSessionContext wrapped session to create a Blob is either use the deprecated method, or use a direct JDBC connection object to build your own. Like this:

Code:
           
      session.doWork(new Work() {      
         @Override
         public void execute(Connection connection) throws SQLException {
            Blob blob = connection.createBlob();
                                // get dataBytes
            java.sql.Blob blob.setBytes(1, dataBytes);
            // now you have a blob to use         
         }
      });
      

_________________
Zemian Deng
------------
Need a Java Scheduler? Try
http://bitbucket.org/timemachine/scheduler


Top
 Profile  
 
 Post subject: Re: Problem with Hibernate.createBlob() in 3.5-Final
PostPosted: Thu Sep 02, 2010 1:02 pm 
Newbie

Joined: Tue Dec 15, 2009 4:56 pm
Posts: 2
Thank you for your reply!

I find another solution - don't use java.sql.Blob at all :-)). I currently use byte[] property type and javassist bytecode instrumentation for field's lazy loading instead.


Top
 Profile  
 
 Post subject: Re: Problem with Hibernate.createBlob() in 3.5-Final
PostPosted: Fri Sep 10, 2010 7:45 am 
Regular
Regular

Joined: Sun Feb 14, 2010 3:29 pm
Posts: 58
Location: USA
I have filed a JIRA issue to track this

http://opensource.atlassian.com/project ... e/HHH-5550

_________________
Zemian Deng
------------
Need a Java Scheduler? Try
http://bitbucket.org/timemachine/scheduler


Top
 Profile  
 
 Post subject: Re: Problem with Hibernate.createBlob() in 3.5-Final
PostPosted: Tue Jan 25, 2011 6:56 am 
Newbie

Joined: Tue Jan 25, 2011 6:52 am
Posts: 1
I believe this would also affect sessions obtained from ManagedSessionContext.currentSession() and JTASessionContext.currentSession(), which return org.hibernate.classic.Session, because that does not extend LobCreationContext either.

In 3.6, session.getLobHelper().createBlob( bytes ), should be used instead.

Zemian, could you give 3.6.0 a try?

_________________
perfumes
handbags
sunglasses
furniture


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