Announcement Announcement Module
Collapse
No announcement yet.
using hibernate criteria and filters Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • using hibernate criteria and filters

    Hi everybody,

    I am using spring with hibernate by extending the HibernateDaoSupport class and by using the getHibernateTemplate() method. But it does no provide any direct way to create hibernate criteria... it requiers me to send a session object which later gives me lots of problems. Also it provides no feature to use filters too. Please help!!!

    thanks in advance.

  • #2
    Here is my usage of Criteria and I see no problems.
    Ok, for using filters you must get Session object, but that's all that is.

    try {
    Criteria criteria = getHibernateTemplate().createCriteria(getSession(t rue), DefaultData1.class);
    criteria.add(Expression.eq("source", source));
    criteria.add(Expression.sql("transfer_status & ? = 0",
    new Integer(status_key),
    Hibernate.INTEGER));
    if (datesValid) {
    criteria.add(Expression.ge("datum", from));
    criteria.add(Expression.le("datum", till));
    }
    criteria.setMaxResults(DATA1_MAX_RESULT_ROWS);

    return criteria.list();
    } catch (HibernateException e) {
    throw convertHibernateAccessException(e);
    }

    Comment


    • #3
      Tried that... but exceptions when i update

      I tried the above method but when I try to update the object of DefaultData1 (from the above example) by either using getHibernateTemplate().update(obj) or getSession().update(obj) I get an exception stating that I am trying to access a colelction with two open sessions.
      Please help!!

      Thanks in advance.

      Comment


      • #4
        I tried the above method but when I try to update the object of DefaultData1 (from the above example) by either using getHibernateTemplate().update(obj) or getSession().update(obj) I get an exception stating that I am trying to access a colelction with two open sessions.
        Please help!!
        Try using the callback mechanism offered by HibernateTemplate like this:

        Code:
        getHibernateTemplate().execute(
        	new HibernateCallback() {
        		public Object doInHibernate(Session session) throws HibernateException {
        			Criteria criteria = createCriteria(session, DefaultData1.class);
        			criteria.add(Expression.eq("source", source));
        			criteria.add(Expression.sql("transfer_status & ? = 0",
        			new Integer(status_key),
        			Hibernate.INTEGER));
        			if (datesValid) {
        				criteria.add(Expression.ge("datum", from));
        				criteria.add(Expression.le("datum", till));
        			}
        			criteria.setMaxResults(DATA1_MAX_RESULT_ROWS);			
        			return criteria.list(); 
        		}
        	}
        );
        If you are not sure what this is about try reading the Hibernate chapter in the Spring reference documentation or chapter 10 of J2EE Development without EJB.

        Comment

        Working...
        X