-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Need help on Criteria API
PostPosted: Fri May 30, 2008 6:13 am 
Newbie

Joined: Fri May 30, 2008 5:28 am
Posts: 2
Hello,

I'm relatively new to this hibernate world! So I started using hibernate as the persistence layer support framework with the final year project of my bachelor's degree. I have used the criteria API to retrieve some data (namely "Surfingmode" values) from "surfingmode" table. I've used criteria API because I wanna execute a some sort of a dynamic query depending on whether user is giving a keyword to be searched. (It's a lookup screen)

Following is my hibernate mapping,

<hibernate-mapping package="miracle.server.persistence">

<class name="SurfingMode" table="surfingmode">
<id name="id" column="id" type="integer">
<generator class="native" />
</id>
<property name="mode" column="mode" type="string"/>
<property name="description" column="description" type="string"/>
<property name="status" column="status" type="char"/>
</class>

<query name="SurfingMode.getAllSurfingModeByName">
<![CDATA[
from SurfingMode as SurfingMode
where SurfingMode.mode =:surfing_mode
]]>
</query>

</hibernate-mapping>

Following is the complete method definition I have written to retrieve data ("surfingmodes")

Code:
public List retrieveSurfingModes(String keyword) throws HibernateException {
        Session selectSession = null;
        Transaction selectTx = null;
        Criteria crit = null;
        List results = null;
        try {
            selectSession = HibernateFactory.openSession();
            selectTx = selectSession.beginTransaction();

            crit = selectSession.createCriteria(SurfingMode.class);

            if (keyword != null && keyword.length() > 0) {
                crit.add(Restrictions.ilike("mode", keyword, MatchMode.START)); // ilike=case insensitive
            }

            crit.add(Restrictions.eq("status", 'Y')); // get only active(NOT DELETED) surfing modes

            ProjectionList plist = Projections.projectionList();
            plist.add(Projections.property("id").as("id"));
            plist.add(Projections.property("mode").as("mode")); // corresponding domain object property name must be passed to as(), otherwise properties of domain objects' of the returning List will be null
            plist.add(Projections.property("description").as("description"));
            crit.setProjection(plist);

            crit.addOrder(Order.asc("mode"));
            crit.setResultTransformer(new AliasToBeanResultTransformer(SurfingMode.class)); // this forces to return a List of domain objects rather than a List of Object[] array list

            results = crit.list();
            selectTx.commit();
            logger.debug("Retrieval done");
        } catch (HibernateException ex) {
            try {
                if (selectTx != null) {
                    selectTx.rollback();
                }
            } catch (HibernateException e) {
                logger.error("Rollback failed", e);
            }
            logger.error("Error occured, Retrieval failed", ex);
            throw ex;
        } finally {
            if (selectSession != null) {
                selectSession.close();
            }
        }

        return results;
    }


This query works fine and give results without any error when no keyword is passed. (When no keyword is given in the lookup,
Code:
crit.add(Restrictions.ilike("mode", keyword, MatchMode.START));
code doesn't get executed ) But whenever this method is called with a keyword, hibernate throws an exception saying "Cannot find field y0_"

Maybe this error occurs due to lack of my knowledge on Criteria API, So I would be grateful if anybody can point out where the problem is.

Thank you.
Regards,
VIRAJ

Hibernate version: 3.2

Name and version of the database you are using: MySQL 5


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 9:50 am 
Newbie

Joined: Fri Apr 04, 2008 2:17 pm
Posts: 15
That suggests to me that your database mapping isn't correct. Perhaps the column doesn't exist in the given table? In some databases the column's case may be important -- so selecting from 'foo' when the column is named 'FOO' might fail. I would double check what the table looks like in the database and confirm it matches your mapping exactly


Top
 Profile  
 
 Post subject: Reply
PostPosted: Sun Jun 01, 2008 9:41 am 
Newbie

Joined: Fri May 30, 2008 5:28 am
Posts: 2
Hello,

There's no error in mapping file..

Quote:
This query works fine and give results without any error when no keyword is passed. (When no keyword is given in the lookup,
Code:
crit.add(Restrictions.ilike("mode", keyword, MatchMode.START));
code doesn't get executed ) But whenever this method is called with a keyword, hibernate throws an exception saying "Cannot find field y0_"



Regards,
Viraj


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 02, 2008 9:14 am 
Newbie

Joined: Fri Apr 04, 2008 2:17 pm
Posts: 15
Turn on SQL output in your hibernate.cfg.xml:
<property name="show_sql">true</property>


The generated SQL will output to the console. What does this look like? That may give some insight


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.