Announcement Announcement Module
No announcement yet.
How to use the getHibernateTemplate() to extract data? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to use the getHibernateTemplate() to extract data?


    i need some advice on how to retrieve data from the database using the getHibernateTemplate.

    Currently i have this statment to execute to extract out the data:
    select * from table limit 10, 20;
    After google for some time, i manage to find the solution to this problem by using findByCriteria and DetachedCritera:

    int start = 10
    int end = 20
    List obj = getHibernateTemplate().findByCriteria(DetachedCriteria.forClass(BlogDaoBean.class), start, end);
    I realise i need to filter the data by the column flag too where i need to modify my statement to:
    select * from table where flag = 0 limit 10, 20;

    How do i implement that statement logic into hibernate? Do i use findByCritera again or if thats another way to do it?

    Thanks for the help..

  • #2
    hi, just some updates, i try to add in some changes but hit this error. Is it because the addExpression is not use in this way? :

    int start = 10
    int end = 20
    DetachedCriteria critera = DetachedCriteria.forClass(BlogDaoBean.class);
    		List obj = getHibernateTemplate().findByCriteria(critera.add(Expression.eq("flag", 0)), counter, credits);
    Error encounter:

    15:01:00,508 ERROR [[action]] Servlet.service() for servlet action threw exception
    org.springframework.orm.hibernate3.HibernateQueryException: could not resolve property: flag of: com.dao.model.resultBean; nested exception is org.hibernate.QueryException: could not resolve property: flag of: com.dao.model.resultBean
    org.hibernate.QueryException: could not resolve property: flag of: com.dao.model.resultBean
    	at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(
    	at org.hibernate.persister.entity.AbstractPropertyMapping.toType(
    	at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(
    	at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(
    	at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(
    	at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(
    	at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(
    	at org.hibernate.criterion.SimpleExpression.toSqlString(
    	at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(
    	at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(
    	at org.hibernate.loader.criteria.CriteriaLoader.<init>(
    	at org.hibernate.impl.SessionImpl.list(
    	at org.hibernate.impl.CriteriaImpl.list(
    	at org.springframework.orm.hibernate3.HibernateTemplate$37.doInHibernate(
    	at org.springframework.orm.hibernate3.HibernateTemplate.execute(
    	at org.springframework.orm.hibernate3.HibernateTemplate.findByCriteria(
    Last edited by vitalstrike82; Apr 28th, 2009, 02:12 AM.


    • #3
      I suggest you buy a book on hibernate and how to use it ( or at least start reading the hibernate reference guide).

      You can only add expressions that way for properties on the mapped object (flag is obviously not a mapped property). So it is either not mapped or the property is named differently on your object.

      If it isnt mapped use a sql statement to limit the results (sqlRestrictions instead of add), if it is mapped fix it to have the proper property name.


      • #4
        Thanks for the information


        • #5
          ok i get it.. manage to solve the problem thanks