Hi, I am trying to execute the following query using DetachedCriteria:
SELECT *, MATCH(postContent, postTitle) AGAINST ("ferrari porsche lambo") as Relevance
FROM POST
WHERE MATCH (postContent, postTitle) AGAINST("ferrari porsche lambo" IN BOOLEAN MODE)
ORDER BY Relevance DESC
As you can see from the query I am implementing FULLTEXT searching against my database. What I need is to use getHibernateTemplate().findByCriteria(criteria, rowNum, itemsPerPage); for pagination purposes.
Does anyone know how to use DetachedCriteria to add the Relevance alias to the query? This is not working due to Relevance not being on the Post bean...which it won't be since I want a dynamic query.
Here is the Java source:
Code:
DetachedCriteria criteria = DetachedCriteria.forClass(Post.class);
criteria.createCriteria("MATCH (postContent, postTitle) AGAINST ('"+searchCriteria+"' IN BOOLEAN MODE) as Relevance");
criteria.add(Restrictions.sqlRestriction("MATCH (postContent, postTitle) AGAINST('"+searchCriteria+"' IN BOOLEAN MODE)"));
criteria.addOrder(OrderBySqlFormula.sqlFormula("Relevance DESC"));
postList = getHibernateTemplate().findByCriteria(criteria, rowNum, itemsPerPage);
This generates the following error since Relevance is not on the Post bean:
org.springframework.orm.hibernate3.HibernateQueryException : could not resolve property: MATCH (postContent, postTitle) AGAINST ('ferrari' IN BOOLEAN MODE) as Relevance of: domain.Post;
I have searched for many hours on this issue with no luck. I basically need a pointer to correct the second line of code in my Java source to properly generate the hibernate query.