Integrate Struts Spring Hibernate web application
The web application to be created using Struts Spring and Hibernate can be configured in 6 easy steps. This configuration is an example to configure hibernate using .hbm files using the HibernateDaoSupport as shown below:
1) Load the Spring application context xml file in struts-config - This can be achieved using the Spring pugin for struts org.springframework.web.struts.ContextLoaderPlugIn class. The jar in which you can find this class is org.springframework.web.struts-3.0.1.RELEASE.jar
3) Configure the SessionFactory of hibernate - This is configured in the spring application context xml file. Configure the bean for SessionFactory using class org.springframework.orm.hibernate.LocalSessionFactoryBean. The class can be found in jar spring-hibernate.jar
The web application to be created using Struts Spring and Hibernate can be configured in 6 easy steps. This configuration is an example to configure hibernate using .hbm files using the HibernateDaoSupport as shown below:
1) Load the Spring application context xml file in struts-config - This can be achieved using the Spring pugin for struts org.springframework.web.struts.ContextLoaderPlugIn class. The jar in which you can find this class is org.springframework.web.struts-3.0.1.RELEASE.jar
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/springconfig/applicationContext.xml"/>
</plug-in>
2) Configure the datasource - This is configured in the spring application context xml file. Configure the bean for datasource using class org.springframework.jdbc.datasource.DriverManagerDataSource. The class can be found in jar spring-jdbc-2.0.7.jar
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url"><value>jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]</value>
</property>
<property name="username"><value>[USERNAME]</value></property>
<property name="password"><value>[PASSWORD]</value></property>
</bean>
3) Configure the SessionFactory of hibernate - This is configured in the spring application context xml file. Configure the bean for SessionFactory using class org.springframework.orm.hibernate.LocalSessionFactoryBean. The class can be found in jar spring-hibernate.jar
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="mappingResources">
<list>
<value>com/javacodeimpl/hibernate/hbm/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">net.sf.hibernate.dialect.OracleDialect</prop>
</props>
</property>
</bean>
4) Configure the .hbm file - In this step, configuration of Person.hbm.xml file is done. Define the configuration for Person table, include the columns that would be required by the application.
<hibernate-mapping>
<class name="com.javacodeimpl.hibernate.entity.Person" table="Person">
<id name="id" column="id" >
<generator class="increment"/>
</id>
<property name="name" column="name" not-null="true"/>
<property name="age" column="age" not-null="true"/>
</class>
</hibernate-mapping>
5) Configure the DAO class - This configuration is done in the application context xml file. It is required to create the bean for the DAO class in which the database operations will be performed.
<bean id="personDAO" class="com.javacodeimpl.dao.hibernate.PersonDAOHibernate">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
6) Create the java DAO class - The java class in which the DB operations are performed. Create methods to get, load, save, update the Person class object with the DB table Person.
public class PersonDAOHibernate extends HibernateDaoSupport implements PersonDAO {
public List getPersons() {
return getHibernateTemplate().find("from Person");
}
public User getPerson(Long id) {
return (Person) getHibernateTemplate().get(Person.class, id);
}
public void saveUser(Person person) {
getHibernateTemplate().saveOrUpdate(person);
}
getHibernateTemplate() - Return the HibernateTemplate for DAO, pre-initialized with the SessionFactory or set explicitly.
No comments:
Post a Comment