Laliwala IT Services

Laliwala IT Services
Website Development

Thursday, March 31, 2011

Liferay tutorial Dynamic Query - hibernate api

Liferay tutorial Dynamic Query

 Liferay action
Liferay services
Liferay tomcat

===============================================================
Writing Dynamic Query using Hibernate API's


http://www.liferay.com/web/guest/community/wiki/-/wiki/Main/Queries+2%3A+DynamicQuery+API

http://www.liferay.com/web/guest/community/wiki/-/wiki/Main/How+to+create+a+custom+query+in+liferay

http://www.liferay.com/web/guest/community/wiki/-/wiki/Main/How+to+create+a+custom+query+in+ext+for+Liferay+models


Forming a Dynamic Query :
=========================
Step 1:

in AddBookAction.java:

if (Validator.isNull(cmd)) {
BookLocalServiceUtil.create(bookTitle);
} else {
List<Book> results = null;
if (cmd.equals(" find" )) {
results = BookLocalServiceUtil.findBooks(bookTitle);
} else {
DetachedCriteria dCriteria = DetachedCriteria.forClass(Book.class);
dCriteria.add(Restrictions.like(" title" , " %" + bookTitle + " %" ));
DynamicQuery dynamicQuery = new DynamicQueryImpl(dCriteria);
results = (List)BookLocalServiceUtil.dynamicQuery(dynamicQuery);
}
req.setAttribute(" results" , results);
req.setAttribute(" cmd" , cmd);
}

Make following imports

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.liferay.portal.kernel.dao.orm.*;
import com.liferay.portal.dao.orm.hibernate.DynamicQueryImpl;


Step 2:
=======
in view.jsp

Add for 'search' button

<input type=" button" value=" Search" onClick=" <portlet:namespace />findBook('search');" />

Note: the findBook JS function is now accepting a parameter, do changes to this function and also to the " Find Book" button.

Step 3:
=======

in Success.jsp update this

if (Validator.isNotNull(cmd)) {
books = (List)request.getAttribute(" results" );
} else {
books = BookLocalServiceUtil.getAllBooks();
}

Step4 :
========
verify we've done all the steps properly
ant deploy from ext-impl,
ant deploy-fast from ext-web,
restart tomcat

Congratulations Liferay developers!!!


No comments:

Post a Comment