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