Announcement Announcement Module
Collapse
No announcement yet.
Spring-Hibernate Query Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring-Hibernate Query

    Hello all,

    i was an application that in Dao implementations with Spring framework:

    Code:
    public class UsersDaoImpl extends JdbcDaoSupport implements UsersDaoDao
    {
      /** Holds users Query Object. */
      private UsersQuery usersQuery;
      ...
    
      protected void initDao() throws ApplicationContextException 
      {
        if (usersQuery == null)
          usersQuery = new UsersQuery(getDataSource());
        ...
      }
      ...
      protected static class UsersQuery extends Query 
      {
        protected UsersQuery(DataSource ds) {
          super(ds, new StringBuffer().append("SELECT ... ").toString());
          compile();
        }
      }
      ...
    }
    like the spring samples, works well. I begin a new application and now the Dao i'm going to implement it with Hibernate.
    I thought that i had to do the same, save the Query like a attribute:
    Code:
    import net.sf.hibernate.Query;
    ...
    public class UsersDaoImpl implements UsersDaoDao
    {
      private SessionFactory sessionFactory;
    
      /** Holds user Query Object. */
      private Query usersQuery;
      ...
    
      protected void initDao() throws ApplicationContextException 
      {
        if(sessionFactory == null) 
        {
          throw new ApplicationContextException("Must set hibernate...");
        }
        this.createUsersQuery();
        ...
      }
      ...
      private void createUsersQuery()
      {
        Session session = SessionFactoryUtils.getSession(sessionFactory, true);
        try {
          StringBuffer select = new StringBuffer();
          select.append("from users in class domain.User");
          this.usersQuery = session.createQuery( select.toString() );
        }
        catch (HibernateException ex) {
          throw SessionFactoryUtils.convertHibernateAccessException(ex);
        }
      }
      ...
      public List findAllUsers() throws DataAccessException
      {
        List listUsers = null;
        try
        {
          listUsers = this.usersQuery.list();
          return listUsers;
        }
        catch (HibernateException ex) 
        {
          throw SessionFactoryUtils.convertHibernateAccessException(ex);
        }
      }
    }
    well, it don't work. i thought that was better create the Queries once because performance.

    when i insert a new user, the Query return the same List.

    how do it this???

    thanks in advance,
    CÚsar.

  • #2
    CÚsar,

    First of all, your classes are not thread safe. You should avoid using private members in your classes to save state.

    i thought that was better create the Queries once because performance.
    Spring/Hibernate behave very well since Hibernate maintains a second level cache where it stores data. Creating a new query each time you need to retrieve some data does not really affects overall performances.

    when i insert a new user, the Query return the same List.
    If the Query is already initialized and populated, query.list() always returns the same data.

    For more information on how to use Spring Integration for Hibernate, please review petclinic from Spring distribution.

    HTH

    Comment


    • #3
      thank you very much,
      CÚsar.

      Comment

      Working...
      X