Hi Everyone I am going to give a simple example to execute
Struts-Hibernate Integration
In this tutorial i am using 1.Mysql, 2.Eclipse Free version from
http://www.eclipse.org/downloads/ 3.Mysql Connector Jar
Everyone know struts is a MVC Architecture and Hibernate is Object relational Mapping tool it provides object/relational persistence and query service for java.So that it minimizes sql statements
Lets Look in to the Example Now Integrating hibernate with struts starts with hibernate configuration.It provides connection information to hibernate see the code below i am using mysql, so i use mysqldialect, if you use any other database use its dialect accordingly.
Quote:<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.pool_size">1</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create-drop</property>
<mapping class="s2s.tablecreation.User" />
</session-factory>
</hibernate-configuration>
Here i am using the default database test of mysql you can change any database.
The below packages are imported to create the table for our purpose using hibernate without writing sql query
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
The validation is done using the packages below
import org.hibernate.validator.Length;
import org.hibernate.validator.NotEmpty;
import org.hibernate.validator.Min;
import org.hibernate.validator.Max;
import org.hibernate.validator.NotNull;
If the imported packages not used then it shows warning to hide the warning use the annonation @SuppressWarnings("unused")
The below code does the creation of the table in DAO implementation class
public void saveUser(User user) {
try {
session.save(user);
} catch (Exception e) {
transaction.rollback();
e.printStackTrace();
}
}
Here User is the table to be created.After the Page is loaded the values are checked by validation once it passes validation it is stored in database otherwise the validation messages can be displayed.We can give those messages either by annotation or by properties file using annotation use @NotEmpty(message="Please Give a valid Email id") like that or In the file "UserAction.properties" we can assign the values.
The packages
import org.hibernate.Session;
import org.hibernate.Transaction;
used to do the transaction for lisitng the values
public List<User> listUser() {
List<User> courses = null;
try {
courses = session.createQuery("from User").list();
} catch (Exception e) {
e.printStackTrace();
}
return courses;
}
The insert page uses the <%@taglib uri="/struts-tags" prefix="s"%> using the prefix the values are get using the textbox and once the value is submitted,the action file uses the setter and getter methods to pass the values.Here we get the three values
I have added the mysqlconnector jar file in lib folder the other jar files listed below you have to add in the lib folder
antlr-2.7.6,commons-collections-3.1,commons-fileupload-1.2.1, commons-io-1.3.2, commons-lang-2.3, commons-logging-1.1, dom4j-1.6.1, ejb3-persistence, freemarker-2.3.13, hibernate3,hibernate-annotations, hibernate-commons-annotations, hibernate-validator, javassist-3.9.0.GA, jta-1.1, junit-3.8.1, log4j-1.2.15, ognl-2.6.11, slf4j-api-1.5.8, slf4j-log4j12-1.5.8, struts2-convention-plugin-2.1.6, struts2-core-2.1.6, struts2-fullhibernatecore-plugin-1.4-GA, xwork-2.1.2
These jar files are combined in
hibernate distribution and
struts distribution so download those distributions or if you use Myeclipse you can add these capabilities automatically.
So just download the Complete Project
Here
After downloading the project import the project using the
method described here .
Any doubt ask us
Happy coding