« Retour - Sample Portlets

Sample Hibernate Portlet

Étiquettes: liferay portlet

This article will explain why the Sample Hibernate Portlet was created, how to install and configure it, and the technical details for how it works.

Overview #

The purpose of the Sample Hibernate Portlet is to provide a simple, bare-bones portlet that inserts and retrieves data from any database via Hibernate. It has the ability to function in 2 modes. First, it can function as a standard portlet that can be added to a page and allows the user to view, add, edit, and delete data. Second, it can also function like a webservice that returns data in an XML via a servlet. More details will be provided in the following sections.

Installation #

  • It is assumed you are using Tomcat as your server and MySQL as your database and $WAR = /trunk/portlets/sample-hibernate-portlet.war
  • Go to $WAR/WEB-INF/sql/sample-hibernate-mysql.sql and run this script to create the test database and test data
  • Go to $WAR/WEB-INF/classes/hibernate.cfg.xml and update the MySQL properties accordingly
  • The build.properties file in the portal root directory has a parameter where you list the WARs you would like to deploy. Create your own properties file to override the default one by creating a file called build.[your computer user name here].properties (eg. build.cmay.properties) and adding this param:
 deploy.wars.includes=sample-hibernate-portlet.war
  • At a command prompt, goto the "portlets" folder in the root directory and run this ant command:
 ant clean deploy
  • This should create a file called "sample-hibernate-portlet.war" in your /home/liferay/deploy directory
  • Start up your Tomcat server, and the WAR should be deployed
  • Log in to the portal and add "Sample Hibernate Portlet" to one of your pages

Technical Details #

In this section, we'll examine how this portlet can run in the 2 modes described above as well as discuss ways to customize this portlet to suit your needs.

Standard Portlet Mode #

After the portlet has been added to a page, you should see a table of the test data as well as buttons for adding, editing, and deleting. Most of the work is done in view.jsp and the com.sample.hibernate.portlet.HibernatePortlet class. Also, the com.sample.hibernate.util.FoodItemUtil class has example Hibernate calls for inserting, updating, and deleting records, as well as retrieving records using custom SQL calls.

Webservice Mode #

Often times, there is a requirement for the portal to access a database, retrieve data from it, and format and display the data. This portlet can be used to do just that. There is a servlet (com.sample.hibernate.servlet.HibernateServlet) that can be used to call a custom implementation class (for this example, com.sample.hibernate.FoodItemComponentImpl) to retrieve the data (using the same FoodItemUtil class described above) and return it as XML. The following is an example call to retrieve food item data:

 http://localhost:8080/sample-hibernate-portlet/servlet?cmd=getFoodItemXml

Parameters can also be passed in to further refine the returned data:

 http://localhost:8080/sample-hibernate-portlet/servlet?cmd=getFoodItemXml&foodItemId=2

In addition, an XSL file can be used in conjunction with the returned XML to format and display the data. For example, you could use the "XSL Content" portlet and use the following values for the XML and XSL parameters respectively:

 **XML:** http://localhost:8080/sample-hibernate-portlet/servlet?cmd=getFoodItemXml
 **XSL:** http://localhost:8080/sample-hibernate-portlet/view.xsl
Moyenne (0 Voter)
L'estimation moyenne est de 0.0 étoiles sur 5.
Commentaires
Réponses Auteur Données
hai Alexander Chow, i understood this sample... Auditya manikanta Vadrevu 24 juillet 2008 22:35
This portlet has some problem in Liferay 6??? ... Melvin Rodriguez Rojas 17 février 2011 12:00
This portlet has some problem in Liferay 6??? ... Melvin Rodriguez Rojas 17 février 2011 12:02
I put slf4j-api-1.5.5.jar and... Matt Walker 15 juillet 2011 16:41
Hello, how i can use an hibernate module jar to... Mario R 30 avril 2014 08:44

hai Alexander Chow,

i understood this sample portlet and its working fine when i linked with postgresql database.

but how do i create my own custom applications using hibernate portlet
Publié le 24/07/08 22:35.
This portlet has some problem in Liferay 6???


An unexpected error occurred.
Please check that these database settings are correct.

You can change the database settings by modifying /WEB-INF/classes/hibernate.cfg.xml.

The SQL script to build the database is found in /WEB-INF/sql/sample-hibernate-mysql.sql.

I already modify the data base settings..


Finally in the eclipse console say this:
R [PortletRequestDispatcherImpl:136] org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.NoClassDefFoundError: Could not initialize class com.liferay.samplehibernate.util.HibernateUtil
Publié le 17/02/11 12:00 en réponse à auditya manikanta vadrevu.
This portlet has some problem in Liferay 6???


An unexpected error occurred.
Please check that these database settings are correct.

You can change the database settings by modifying /WEB-INF/classes/hibernate.cfg.xml.

The SQL script to build the database is found in /WEB-INF/sql/sample-hibernate-mysql.sql.

I already modify the data base settings..


Finally in the eclipse console say this:
R [PortletRequestDispatcherImpl:136] org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.NoClassDefFoundError: Could not initialize class com.liferay.samplehibernate.util.HibernateUtil
Publié le 17/02/11 12:02.
I put slf4j-api-1.5.5.jar and slf4j-jdk14-1.5.5.jar into WEB-INF/lib and it fixed it
Publié le 15/07/11 16:41 en réponse à Melvin Rodriguez Rojas.
Hello, how i can use an hibernate module jar to share data between portlets?
Actually the records added in portlet A do not refresh on portlet B..
Thanks
Publié le 30/04/14 08:44.