Announcement Announcement Module
Collapse
No announcement yet.
Which the better way to keep SessionFactory in Hibernate? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      Thank for your guidance!

      I'm still refactoring some codes now.

      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.

      Comment

      Working...
      X