Results 1 to 3 of 3

Thread: Which the better way to keep SessionFactory in Hibernate?

  1. #1
    Join Date
    Mar 2012
    Posts
    9

    Default Which the better way to keep SessionFactory in Hibernate?

    Hi everyone,

    I'm a newbie in both Spring and Hibernate. When i was looking for a way to handle SessionFactory, i saw this link: http://static.springsource.org/sprin...rence/orm.html. It saids:

    12.2.2. SessionFactory setup in a Spring container

    To avoid tying application objects to hard-coded resource lookups, Spring allows you to define resources such as a JDBC DataSource or a Hibernate SessionFactory as beans in the Spring container. Application objects that need to access resources just receive references to such pre-defined instances via bean references (the DAO definition in the next section illustrates this).
    Anyone can show me why are hard-coded resource lookups not good?
    And more, in some popular Hibernate tutorials, it seems many one used a class like HibernateUtils to create and keep SessionFactory in static variables? Meanwhile, the docs saids:

    12.2.5. Implementing DAOs based on plain Hibernate 3 API

    Hibernate 3 provides a feature called "contextual Sessions", where Hibernate itself manages one current Session per transaction. This is roughly equivalent to Spring's synchronization of one Hibernate Session per transaction. A corresponding DAO implementation looks like as follows, based on the plain Hibernate API:

    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();
    }
    }

    This style is very similar to what you will find in the Hibernate reference documentation and examples, except for holding the SessionFactory in an instance variable. We strongly recommend such an instance-based setup over the old-school static HibernateUtil class from Hibernate's CaveatEmptor sample application. (In general, do not keep any resources in static variables unless absolutely necessary.)
    What's better way for performance?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    Performance wise there shouldn't be an issue or difference, coding wise the way as shown in the reference guide is the correct way. You really don't want to do these ugly lookups in your code with some crappy HibernateUtils class, it is nearly impossible to test/differentatiate between environments, creating a stub/mock for the sessionfactory/entitymanager isn't as easy.

    I suggest you read more about Dependency Injection and/or Inversion of Control and why that is important in your application.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Mar 2012
    Posts
    9

    Default

    Thank for your guidance!

    I'm still refactoring some codes now.

    Quote Originally Posted by Marten Deinum View Post
    Performance wise there shouldn't be an issue or difference, coding wise the way as shown in the reference guide is the correct way. You really don't want to do these ugly lookups in your code with some crappy HibernateUtils class, it is nearly impossible to test/differentatiate between environments, creating a stub/mock for the sessionfactory/entitymanager isn't as easy.

    I suggest you read more about Dependency Injection and/or Inversion of Control and why that is important in your application.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •