Results 1 to 3 of 3

Thread: Spring-Hibernate Query

  1. #1

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    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
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3

    Default

    thank you very much,
    César.

Similar Threads

  1. Spring Query API
    By scottr in forum Data
    Replies: 8
    Last Post: Jul 28th, 2010, 02:14 PM
  2. Replies: 4
    Last Post: Jul 28th, 2010, 02:10 PM
  3. Replies: 5
    Last Post: Feb 3rd, 2009, 05:19 AM
  4. Spring + Hibernate ORA-00936: missing expression
    By Hugh_la_Main in forum Data
    Replies: 1
    Last Post: Jun 28th, 2005, 08:48 AM
  5. Replies: 14
    Last Post: Feb 21st, 2005, 05:41 PM

Posting Permissions

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