Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Struts-Hibernate Integration tutorial to insert values
12-13-2011, 01:02 AM (This post was last modified: 12-13-2011 01:42 AM by rajasri.)
Post: #1
Photo Simple Struts-Hibernate Integration tutorial to insert values
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
  • Name
  • Age
  • Email

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
Find all posts by this user
Add Thank You Quote this message in a reply
01-08-2012, 11:39 PM
Post: #2
RE: Simple Struts-Hibernate Integration tutorial to insert values
good tutorial
Find all posts by this user
Add Thank You Quote this message in a reply
01-15-2012, 06:10 PM
Post: #3
RE: Simple Struts-Hibernate Integration tutorial to insert values
its very useful
Find all posts by this user
Add Thank You Quote this message in a reply
Post Reply 


Possibly Related Threads...
Thread: Author Replies Views: Last Post
  Login form authentication using Struts with database mjm 0 302 01-22-2012 02:52 AM
Last Post: mjm
Thumbs Up Struts hiberanate integration tutorial part2 rajasri 7 907 01-20-2012 01:03 AM
Last Post: mjm
Smile Error while initializing hibernate: An AnnotationConfiguration instance is required t rajasri 0 292 12-14-2011 05:15 PM
Last Post: rajasri
Question Hibernate Errors and Solutions rajasri 0 116 12-14-2011 03:49 PM
Last Post: rajasri
Sad Simple Struts Example rajasri 0 142 12-14-2011 03:47 PM
Last Post: rajasri
Tongue Struts Dispatch Action Tutorial with example rajasri 0 580 12-13-2011 01:26 AM
Last Post: rajasri
Photo How to export data as pdf in struts using displaytags rajasri 0 390 12-12-2011 11:08 PM
Last Post: rajasri
  Struts hiberanate integration tutorial part2 admin 0 142 11-29-2011 03:48 AM
Last Post: admin
  Get start Hibernate admin 0 178 11-19-2011 01:35 AM
Last Post: admin

Forum Jump:


User(s) browsing this thread: 2 Guest(s)


WE will share important jobs and updates on facebook so like us to get updates