Hi, I'm wondering how to order my result set when I am mapping a UserType. I am using a variation of the Null Object pattern to save/load times that evaluate to "the future".
So, to do that, I mapped my time using two columns: propertyName_is_future and propertyName_datetime. Now, I'd like to retrieve my objects ordered by descending datetime. I get a QueryException, detailed below.
First of all, is that even possible ? Second, if so, how ? Third, anyone has a simple solution ? I also need to use setMaxResults() to only retrieve the first few entries of the ordered set.
Thanks for any tips !
François
Hibernate version:
2.1.7
Mapping documents:
Code:
<property name="postedOn"
type="com.careernudge.dao.hibernate.type.InstantType">
<column name="postedOn_is_future" sql-type="bit"/>
<column name="postedOn_datetime" sql-type="datetime"/>
</property>
InstantType class excerpt:
Code:
public Object nullSafeGet(ResultSet rs, String[] columnNames,
Object command) throws HibernateException,
SQLException {
boolean future = rs.getBoolean(columnNames[0]);
if (rs.wasNull()) {
return null;
} else if (future) {
return FutureInstant.SOLE_INSTANCE;
}
Date instant = rs.getDate(columnNames[1]);
return new Instant(instant);
}
Instant is part of
Joda Time. I'm using v0.98 of Joda Time.
Code between sessionFactory.openSession() and session.close():Code:
Query q = session.createQuery(
"from HibernateBlogEntry e " +
"order by e.postedOn.datetime desc");
q.setMaxResults(numberOfEntries);
return q.list();
Second query attemptI also tried the following query, but then I get a different exception:
Code:
Query q = session.createQuery(
"from HibernateBlogEntry e " +
"order by e.postedOn desc");
q.setMaxResults(numberOfEntries);
return q.list();
Code:
2005-02-09 21:27:39 StandardWrapperValve[careernudge]: Servlet.service() for servlet careernudge threw exception
org.springframework.orm.hibernate.HibernateQueryException: path expression ends in a composite value: hibernateb0_.postedOn [from com.careernudge.dao.hibernate.HibernateBlogEntry e order by e.postedOn desc]; nested exception is net.sf.hibernate.QueryException: path expression ends in a composite value: hibernateb0_.postedOn [from com.careernudge.dao.hibernate.HibernateBlogEntry e order by e.postedOn desc]
net.sf.hibernate.QueryException: path expression ends in a composite value: hibernateb0_.postedOn [from com.careernudge.dao.hibernate.HibernateBlogEntry e order by e.postedOn desc]
at net.sf.hibernate.hql.PathExpressionParser.getWhereColumn(PathExpressionParser.java:375)
at net.sf.hibernate.hql.OrderByParser.token(OrderByParser.java:31)
at net.sf.hibernate.hql.ClauseParser.token(ClauseParser.java:87)
at net.sf.hibernate.hql.PreprocessingParser.token(PreprocessingParser.java:123)
at net.sf.hibernate.hql.ParserHelper.parse(ParserHelper.java:29)
at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:149)
at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:138)
at net.sf.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:295)
at net.sf.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1571)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1542)
at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39)
at com.careernudge.dao.hibernate.HibernateBlogDao$1.doInHibernate(HibernateBlogDao.java:26)
at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:200)
at org.springframework.orm.hibernate.HibernateTemplate.executeFind(HibernateTemplate.java:220)
at com.careernudge.dao.hibernate.HibernateBlogDao.findMostRecentEntries(HibernateBlogDao.java:18)
Full stack trace of any exception that occurs:Code:
2005-02-09 21:10:51 StandardWrapperValve[careernudge]: Servlet.service() for servlet careernudge threw exception
org.springframework.orm.hibernate.HibernateQueryException: dereferenced: hibernateb0_.postedOn.datetime [from com.careernudge.dao.hibernate.HibernateBlogEntry e order by e.postedOn.datetime desc]; nested exception is net.sf.hibernate.QueryException: dereferenced: hibernateb0_.postedOn.datetime [from com.careernudge.dao.hibernate.HibernateBlogEntry e order by e.postedOn.datetime desc]
net.sf.hibernate.QueryException: dereferenced: hibernateb0_.postedOn.datetime [from com.careernudge.dao.hibernate.HibernateBlogEntry e order by e.postedOn.datetime desc]
at net.sf.hibernate.hql.PathExpressionParser.token(PathExpressionParser.java:140)
at net.sf.hibernate.hql.ParserHelper.parse(ParserHelper.java:29)
at net.sf.hibernate.hql.OrderByParser.token(OrderByParser.java:30)
at net.sf.hibernate.hql.ClauseParser.token(ClauseParser.java:87)
at net.sf.hibernate.hql.PreprocessingParser.token(PreprocessingParser.java:123)
at net.sf.hibernate.hql.ParserHelper.parse(ParserHelper.java:29)
at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:149)
at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:138)
at net.sf.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:295)
at net.sf.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1571)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1542)
at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39)
at com.careernudge.dao.hibernate.HibernateBlogDao$1.doInHibernate(HibernateBlogDao.java:26)
at org.springframework.orm.hibernate.HibernateTemplate.execute(HibernateTemplate.java:200)
at org.springframework.orm.hibernate.HibernateTemplate.executeFind(HibernateTemplate.java:220)
at com.careernudge.dao.hibernate.HibernateBlogDao.findMostRecentEntries(HibernateBlogDao.java:18)
at com.careernudge.web.filters.RecentBlogEntriesFinderFilter.doFilter(RecentBlogEntriesFinderFilter.java:29)
Name and version of the database you are using:HSQLDB 1.7.3.2
The generated SQL (show_sql=true):None, that's why I'm asking :)
Debug level Hibernate log excerpt:Code:
2005-02-09 21:20:21,734 [DEBUG] impl.SessionImpl - opened session
2005-02-09 21:20:21,750 [DEBUG] impl.SessionImpl - find: from HibernateBlogEntry e order by e.postedOn.datetime desc
2005-02-09 21:20:21,765 [DEBUG] engine.QueryParameters - named parameters: {}
2005-02-09 21:20:21,781 [DEBUG] hql.QueryTranslator - compiling query
2005-02-09 21:20:21,796 [DEBUG] impl.SessionImpl - closing session
2005-02-09 21:20:21,796 [DEBUG] impl.SessionImpl - running Session.finalize()