Archive for July, 2010
about:config trick for window.close() In Firefox
Wednesday, July 7th, 2010Integrating Hibernate with Struts1.x
Friday, July 2nd, 2010| We have got list of post(s) about Hibernate. Today as a next step we’ll try to intergrate with MVC framework. We’ll take the most used one which Struts1.3. For example we’ll take the same Singer Example and we’ll work with that for intergrating with struts also.Since we have the domain object generated already, we can make use of the same domain object to populate the values from DB, when we use the Hibernate. |
| We just take the examples of listing all the Singers from Singers table. Since we are using Struts1.x, we may need to write the action class and form class. The following code shows Form Class. Since we are going to list all the Singers, then our Form looks like the following. |
package com.techmaddy.form;
/*
* Copyright 2010 - Kumarasamy
* kumarasamy@techmaddy.com
*/
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import com.techmaddy.singer.Singer;
public class SingerForm extends ActionForm {
private static final long serialVersionUID = 1L;
private Singer[] singer = new Singer[0];
public Singer[] getSinger() {
return singer;
}
public void setSinger(Singer[] singer) {
this.singer = singer;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
singer = new Singer[0];
}
}
|
| The DAO class will make a call to DB and returns the results. This is almost very similar to the class CRUDTest.java in Simple CRUD Operation With Hibernate. Instead of my CRUDTest class, we have an DAO Class which gets the data from DB. |
| My DAO Class looks like the following. |
package com.techmaddy.bo;
/*
* Copyright 2010 - Kumarasamy
* kumarasamy@techmaddy.com
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.Query;
import com.techmaddy.singer.HibernateSessionFactory;
import com.techmaddy.singer.Singer;
public class SingerManager {
public Singer[] getAllSingers() {
List singer = new ArrayList();
Session session = null;
Transaction tx = null;
session = HibernateSessionFactory.getSession();
tx = session.beginTransaction();
Query qry = session.createQuery("from Singer");
for (Iterator iterator = qry.iterate(); iterator.hasNext();) {
singer.add(iterator.next());
}
return (Singer[]) singer.toArray(new Singer[0]);
}
}
|
| The same way, based in the action performed from the screen, we can make a call to Editing and deleting the Singers.The following Images shows the result screen. |
Download To download the War file for Singer with Struts Integration Click Here |












