1. Project structure
C:\workspace\testWebApp>tree
Folder PATH listing
Volume serial number is BED9-803F
C:.
├───.settings
├───src
│ └───main
│ ├───java
│ │ └───com
│ │ └───xyz
│ │ └───persistence
│ ├───resources --copy *.hbm.xml and hibernate.cfg.xml here
│ └───webapp
│ ├───META-INF
│ └───WEB-INF
└───target
2. Create Status Java POJO class - omitted getters and setters
3. Create status.hbm.xml
4. Create hibernate.cfg.xml
5. Create a test driver class (DAO)
6. HibernateUtil to create/close sessions
7. Create index.jsp that calls test driver
8. Copy Oracle DB driver class to application server or package it within war file for this work
9. pom.xml
C:\workspace\testWebApp>tree
Folder PATH listing
Volume serial number is BED9-803F
C:.
├───.settings
├───src
│ └───main
│ ├───java
│ │ └───com
│ │ └───xyz
│ │ └───persistence
│ ├───resources --copy *.hbm.xml and hibernate.cfg.xml here
│ └───webapp
│ ├───META-INF
│ └───WEB-INF
└───target
2. Create Status Java POJO class - omitted getters and setters
import java.io.Serializable;
import java.util.Date;
public class Status implements Serializable {
public Status() {
super();
}
private String statusCode;
private String statusDescr;
private Integer severity;
private Date createTimestamp;
private String createUserId;
private String updUserId;
private Date updTimestamp;
3. Create status.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.xyz.persistence">
<class name="Status" table="AUTH_STAT" mutable="false">
<id name="statusCode" column="AUTH_STAT_CD" type="string"/>
<property name="statusDescr" column="AUTH_STAT_DSC" type="string"/>
<property name="severity" column="AUTH_SEREVITY_ID" type="integer"/>
<property name="createTimestamp" column="CREATE_TS" type="timestamp"/>
<property name="createUserId" column="CREATE_USER_ID" type="string"/>
<property name="updUserId" column="UPD_USER_ID" type="string"/>
<property name="updTimestamp" column="UPD_TS" type="timestamp"/>
</class>
</hibernate-mapping>
4. Create 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.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@dbhost.company.com:20009:sid</property>
<property name="hibernate.connection.username">userName</property>
<property name="hibernate.connection.password">password</property>
<property name="show_sql">true</property>
<property name="hibernate.default_schema">SID</property>
<mapping resource="Status.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
5. Create a test driver class (DAO)
public class TestDriver {
public static void main(String[] args) {
TestDriver driver = new TestDriver();
try {
driver.create();
driver.retrieve();
driver.update();
driver.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
public TestDriver() {
super();
}
public void create() throws Exception {
Status status = createStatus();
// create a new status in db
Session session = createStatus(status);
// retrieve a status from db
Status retrievedStatus = (Status) session.get(Status.class, "RR");
// delete a status
deleteStatus(session, retrievedStatus);
HibernateUtil.shutdown();
}
public void retrieve() throws Exception {
create();
}
public void update() throws Exception {
Status status = createStatus();
// create a new status in db
Session session = createStatus(status);
// retrieve a status
Status retrievedStatus = (Status) session.get(Status.class, "RR");
// update a status
updateStatus(session);
// delete a status
deleteStatus(session, retrievedStatus);
HibernateUtil.shutdown();
}
public void delete() throws Exception {
Status status = createStatus();
Session session = createStatus(status);
// retrieve a status
Status retrievedStatus = (Status) session.get(Status.class, "RR");
// update a status
updateStatus(session);
// delete a status
deleteStatus(session, retrievedStatus);
HibernateUtil.shutdown();
}
public void runCRUDTestCases() throws Exception {
}
private Status createStatus() {
Status status = new Status();
status.setStatusCode("RR");
status.setCreateTimestamp(new Date());
status.setCreateUserId("sss");
status.setSeverity(new Integer(22));
status.setStatusDescr("testing-ssss");
status.setUpdTimestamp(new Date());
status.setUpdUserId("ssss");
return status;
}
private Session createStatus(Status status) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.save(status);
session.getTransaction().commit();
return session;
}
private void updateStatus(Session session) {
Status retrievedStatus;
retrievedStatus = (Status) session.get(Status.class, "RR");
retrievedStatus.setStatusDescr("updated description");
session.beginTransaction();
session.update(retrievedStatus);
session.getTransaction().commit();
}
private void deleteStatus(Session session, Status retrievedStatus) {
session.beginTransaction();
session.delete(retrievedStatus);
session.getTransaction().commit();
}
6. HibernateUtil to create/close sessions
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
7. Create index.jsp that calls test driver
<?xml version="1.0" encoding="UTF-8" ?>
<%@ page import="com.xyz.persistence.TestDriver"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Hibernate + Oracle 11g DB CRUD test page</title>
</head>
<body>
<%
try {
TestDriver driver = new TestDriver();
driver.runCRUDTestCases();
driver.create();
out.println("Create Status succeeded");
out.println("<br>");
driver.retrieve();
out.println("Retrieve Status succeeded");
out.println("<br>");
driver.update();
out.println("Update Status succeeded");
out.println("<br>");
driver.delete();
out.println("Delete Status succeeded");
out.println("<br>");
out.println("All CRUD operations succeeded");
} catch (Exception e) {
out.println("An exception occurred: " + e.getMessage());
}
%>
</body>
</html>
8. Copy Oracle DB driver class to application server or package it within war file for this work
9. pom.xml
<artifactId>testWebApp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>testWebApp Maven Webapp</name>
<url>http://maven.apache.org</url>
<build>
<finalName>testWebApp</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<source>1.4</source>
<target>1.4</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<!-- Project dependencies -->
<dependencies>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.2.GA</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
No comments:
Post a Comment