-->
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.  [ 4 posts ] 
Author Message
 Post subject: wrapping HibernateException into DataAccessException
PostPosted: Tue Mar 09, 2004 12:09 pm 
Beginner
Beginner

Joined: Tue Nov 18, 2003 10:16 am
Posts: 33
Location: Cluj-Napoca, Romania
I am using Spring's transaction management and also exception handling with Hibernate.

All my DAO's throws only org.springframework.dao.DataAccessException.
But, when i am using
Code:
HibernateTemplate().createQuery()
method, this throws net.sf.hibernate.HibernateException, so this exception is not automatically wrapped into a DataAccessException.

Of course, I can try myself to wrap exceptions programatically with org.springframework.orm.hibernate.SessionFactoryUtils convertHibernateAccessException method.

Is there any other way to do it??


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 11, 2004 11:51 am 
Senior
Senior

Joined: Wed Aug 27, 2003 6:04 am
Posts: 161
Location: Linz, Austria
HibernateTemplate's createQuery is not like the other convenience methods in HibernateTemplate: It is intended for use within a HibernateCallback - its javadoc briefly states this. That's why it takes a Session as parameter and why it throws HibernateException.

Code:
public Query createQuery([b]Session session[/b], String queryString) [b]throws HibernateException[/b];


It creates a Query that is aware of a transaction timeout; if you don't use such timeouts, it is equivalent to the Hibernate Session's createQuery.

Code:
List result = getHibernateTemplate().executeFind(
  new HibernateCallback() {
    Object doInHibernate(Session session) throws HibernateException {
      Query query = getHibernateTemplate().createQuery("...");
      // Query query = session.createQuery("...");
      query...;
      return query.list();
    }
  }
);


In general, HibernateTemplate's convenience methods are mainly intended for one-step data access operations. Any more complex operations, like building a Query or Criteria, or performing a sequence of steps, should be implemented in a HibernateCallback.

Juergen


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 29, 2005 2:57 am 
Newbie

Joined: Sun Mar 20, 2005 9:03 pm
Posts: 3
Quote:
In general, HibernateTemplate's convenience methods are mainly intended for one-step data access operations. Any more complex operations, like building a Query or Criteria, or performing a sequence of steps, should be implemented in a HibernateCallback.

Besides performance issues, is there a reason why? Will I not get the same benefits regardless of whether I'm performing operations on the Session or the HibernateTemplate which wraps the session? I realize the HibernateTemplate does some extra work on initialization of the Session, but isn't the bottom line that successive HibernateTemplate operations will always use the same session?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 29, 2005 3:07 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Post on the Spring forum, this is a Hibernate forum.


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