-->
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.  [ 2 posts ] 
Author Message
 Post subject: Using just the Spring AOP, and not any other Spring stuff
PostPosted: Fri Apr 06, 2007 4:47 pm 
Newbie

Joined: Sun Apr 09, 2006 4:57 pm
Posts: 10
Location: Milan (Italy)
First of all, I want to say that I red the Java Persistence book and the http://hibernate.org/42.html document.

I opened this thread hoping that Christian can answer me about a general question about the too-old-do-not-use-spring-stuff discussion.

I agree with Christian, I do not find so useful to hide the specific Hibernate Exceptions behind a generic Exception Hierarchy. So I do not use HibernateTemplate.

But as mentioned in the 42.html document "... To remove transaction demarcation from your data access code you might want to write your own interceptor that can begin and end a transaction programmatically (or even declaratively)....".

In the current documentation of Spring 2.0 they suggest a way to implement a DAO based on Hibernate3 API, where Spring role would be just to inject a SessionFactory.

Here is the example code:
Code:
public class ProductDaoImpl implements ProductDao {
  private SessionFactory sessionFactory;
  public void setSessionFactory(SessionFactory sessionFactory) {
    this.sessionFactory = sessionFactory;
  }

  public Collection loadProductsByCategory(String category) {
     return this.sessionFactory.getCurrentSession()
     .createQuery("from test.Product product where product.category=?")
     .setParameter(0, category)
     .list();
  }
}


In this way, they say, you can still use the Spring AOP to manage the transaction demarcation, without using any other Spring-Hibernate machinery a part the fact that the SessionFactory is injected by the Spring framework using the class LocalSessionFactoryBean (which, anycase, looking at the code, seems to use a standard Hibernate API).

In your experience, as an Hibernate expert, is it a satisfactory way of working if, for some reason, you are forced to use a Spring environment instead of Seam or EJB3? Do you know any problem encountered by people that used this approach?

As you can understand, I cannot formulate this question to the Spring people, as they would not, probably, point out any problem in a solution that they propose.

Thanks a lot
Marco Pancotti (Italy)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 06, 2007 5:11 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
I don't think anyone here (even Christian ;) ) would say that Spring has no place in any Java program, ever. I am in agreement with Christian in that there is no need to use the HibernateTemplate for anything with the 3.1+ release, but I make extensive use of the IoC container to wire together my DAOs and service layers. Additionally, I thoroughly employ the transaction demarcation using Spring and AOP interceptors, it's really rather handy.

IMO, you should always attempt to maintain as few dependencies on external sources in your implementation classes as possible. My UserHibernateDao (implementing the UserDao, of course) should not have dependencies on both Hibernate and Spring, there's just no need for it. Since transaction demarcation on service layers (for instance) is a cross-cutting concern, it is ideal for AOP interception, either through pointcutting expressions or annotations.


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