Results 1 to 5 of 5

Thread: How to use the getHibernateTemplate() to extract data?

  1. #1

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

    Greetings,

    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:

    Code:
    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. #2

    Default

    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? :

    Code:
    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:

    Code:
    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(AbstractPropertyMapping.java:43)
    	at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:37)
    	at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1282)
    	at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:31)
    	at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1257)
    	at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:433)
    	at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTranslator.java:393)
    	at org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:45)
    	at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:333)
    	at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:82)
    	at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:67)
    	at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1533)
    	at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
    	at org.springframework.orm.hibernate3.HibernateTemplate$37.doInHibernate(HibernateTemplate.java:988)
    	at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:366)
    	at org.springframework.orm.hibernate3.HibernateTemplate.findByCriteria(HibernateTemplate.java:978)
    Last edited by vitalstrike82; Apr 28th, 2009 at 02:12 AM.

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4

    Default

    Thanks for the information

  5. #5

    Default

    ok i get it.. manage to solve the problem thanks

Posting Permissions

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