Below process depicts integration of Hibernate in J2EE Project. When user access the URL the application will insert one employee record to the Employee table and display all the records on the web page. It created new Employee table if it does not exists.
Prerequisites:
1. Download hibernate libraries as shown below (hibernate3.jar and other dependencies)
2. Create new database called "ormtest" in mySql Database (change below hibernate.cfg.xml accordingly) (Refer -> How to install mySql and using it within Java ?)
3. Application Server to deploy the application
Project Structure (Eclipse J2EE Project template):
Testing:
- JBoss is running on 8080 port where application is deployed
http://localhost:8080/DemoHibernate/employeeHandler.do
Output: Creates new table Employee (if already not exists) and add one record to it
Resource Reference:
(1) EmployeeHandler
package control;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.* ;
import model.Employee;
import org.hibernate.*;
public class EmployeeHandler extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException{
PrintWriter out = res.getWriter();
//add new record
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Employee emp = new Employee();
emp.setFname("Ichiro");
emp.setLname("Suzuki");
session.save(emp);
//fetch record
List employees = session.createQuery("from Employee").list();
out.println( "Employee List size " + employees.size());
for (Iterator iter = employees.iterator(); iter.hasNext(); ) {
Employee employee = (Employee)iter.next();
out.println( employee.getFname() );
}
tx.commit();
session.close();
}
}
--------------------------------------------------------------------
(2) HibernateUtil.java
--------------------------------------------------------------------
(2) HibernateUtil.java
package control;
import org.hibernate.*;
import org.hibernate.cfg.*;
/**
* Utility to get handle of Hibernate session, which is require to deal with underlying Datasource
* @author santoshlg
*
*/
public class HibernateUtil {
private static SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
} catch (Throwable ex) {
ex.printStackTrace();
}
}
/**
* Returns SessionFactory object
* @return session
*/
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* Releases SessionFactory
*/
public static void shutdown() {
getSessionFactory().close();
}
}
--------------------------------------------------------------------
--------------------------------------------------------------------
(3) Employee.java
package model;
package model;
/**
* Model to hold Employee Information
*/
public class Employee {
private Long id;
private String fname;
private String lname;
public Employee() {}
/**
* @return the lname
*/
public String getLname() {
return lname;
}
/**
* @param lname the lname to set
*/
public void setLname(String lname) {
this.lname = lname;
}
/**
* @return the id
*/
public Long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Long id) {
this.id = id;
}
/**
* @return the fname
*/
public String getFname() {
return fname;
}
/**
* @param fname the fname to set
*/
public void setFname(String fname) {
this.fname = fname;
}
}
--------------------------------------------------------------------
(4) Employee.hbm.xml
(5) hibernate.cfg.xml
(6) web.xml
--------------------------------------------------------------------
(4) Employee.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="model.Employee" table="Employee">
<id name="id" column="EMP_ID">
<generator class="native"/>
</id>
<property name="lname" column="LNAME"/>
<property name="fname" column="FNAME"/>
</class>
</hibernate-mapping>
--------------------------------------------------------------------(5) hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/ormtest</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="show_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.format_sql">true</property>
<!-- Mapping files -->
<mapping resource="/model/Employee.hbm.xml" />
</session-factory>
</hibernate-configuration>
--------------------------------------------------------------------(6) web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>EmployeeHandler</servlet-name>
<servlet-class>control.EmployeeHandler</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EmployeeHandler</servlet-name>
<url-pattern>/employeeHandler.do</url-pattern>
</servlet-mapping>
</web-app>
--------------------------------------------------------------------
Santosh
ReplyDeleteHere is an ORM that works with MySQL
https://www.kellermansoftware.com/p-47-net-data-access-layer.aspx
This is Great.loves to like read your post.Keep it up. Please Visit:
ReplyDeletecafe pos system
cloud pos
pos app
یکی از بهترین روش برای افزایش لایک پستتان، خرید لایک ایرانی از سایت مای ممبر است
ReplyDeletehttps://mymember.shop/خرید-لایک-اینستاگرام
This comment has been removed by the author.
ReplyDelete