I am trying to retrieve a list of events, returning only specific column name with filtering restrictions on certain column. Therefore, I am utilizing Projections.property and Restrictions.like
I am trying to return event of activity like '%ball%' and venue like '%stadium%'. Following are my mapping classes, hibernate call as well as the error message.
It seems that Hibernate is unable to identify the venue column. If I remove "criteria.add(Restrictions.like("venue","stadium", MatchMode.ANYWHERE));" or If I remove ProjectionList pl = Projections.projectionList();, , I will be able to get result.
Could someone kindly advise the what went wrong. Thanks in advance.
// Classes ================== public class Event { private Long eventId; private Activity activity; private Date startTime; private String venue; private String additional; }
public class Activity { private Long activityId; private String activityName; }
// Hibernate Method ===================== public List getEventList() { DetachedCriteria criteria = DetachedCriteria.forClass(Event.class); // define fields to return ProjectionList pl = Projections.projectionList(); pl.add(Projections.property("eventId").as("eventId")); pl.add(Projections.property("activity").as("activity")); pl.add(Projections.property("startTime").as("startTime")); pl.add(Projections.property("venue").as("venue")); criteria.setProjection(pl).setResultTransformer(new AliasToBeanResultTransformer(Event.class)); // join class criteria.createAlias("activity","activity");
// filter restrictions criteria.add(Restrictions.like("activity.activityName","ball", MatchMode.ANYWHERE)); criteria.add(Restrictions.like("venue","stadium", MatchMode.ANYWHERE)); return getHibernateTemplate().findByCriteria(criteria); }
// Error ========================== Hibernate: select this_.eventId as y0_, this_.activityId as y1_, this_.startDateTime as y2_, this_.venue as y3_ from Event this_ inner join Activity ac1_ on this_.activityId=ac1_.activityId where y3_ like ?
Root cause of ServletException. org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query ... org.hibernate.exception.SQLGrammarException: could not execute query .. java.sql.SQLException: [BEA][SQLServer JDBC Driver][SQLServer]Invalid column name 'y3_'.
|