Hi,
I'm relatively new to Hibernate and have been experimenting with whether I could have web applications from different web context accessing the same database via hibernate. I seem to be having problems with seeing changes which are made via one web app on another web app.
I have spent a long time trying to figure out what is wrong with my application so I created two small webapp just to test out how I am using Hibernate instead. Turns out that it didnt work as well.
Basically, I'm trying to do update to the database on one web app, and viewing the update from another. I create 2 web projects "Inserter" and "Viewer". Inserter is used to add new entries to the db. When I try viewing the result through Viewer web app, it doesnt show. I suppose its to do with the persistence objects which are maintained separately between the two apps?
I'm hoping that someone could advise on what I need to do in order to be able to access the same hibernate persistent objects from two different web apps. Below are my test codes.
/Inserter/index.jsp:
Code:
<%@page
import="java.util.Iterator"
import="org.hibernate.Session"
import="org.hibernate.Transaction"
import="org.hibernate.Query"
import="entity.HibernateUtil"
import="entity.User"
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="process/doInsert.jsp">
Name: <input type="text" name="name"/>
Surname: <input type="text" name="surname"/>
<input type="submit" value="Insert"/>
</form>
<table border="1">
<tr><td>Id</td><td>Name</td><td>Surname</td></tr>
<%
Session sess = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = null;
try {
tx = sess.beginTransaction();
String SQL_QUERY = "from User s";
Query query = sess.createQuery(SQL_QUERY);
for (Iterator it = query.iterate(); it.hasNext();) {
User newuser = (User) it.next();
%>
<tr><td><%=newuser.getId()%></td><td><%=newuser.getName()%></td><td><%=newuser.getSurname()%></td></tr>
<%
}
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
//sess.close();
}
%>
</table>
</body>
</html>
/Inserter/process/doInsert.jsp:
Code:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@page
import="org.hibernate.Session"
import="org.hibernate.Transaction"
import="org.hibernate.Query"
import="entity.HibernateUtil"
import="entity.User"
%>
<%
String name = request.getParameter("name");
String surname = request.getParameter("surname");
System.out.println("Name: " + name + ", Surname: " + surname);
Session sess = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = null;
try {
tx = sess.beginTransaction();
User newuser = new User(name, surname);
sess.saveOrUpdate(newuser);
sess.persist(newuser);
tx.commit();
} catch (Exception e) {
if (tx!=null)
tx.rollback();
System.out.println(e.getMessage());
} finally {
//sess.close();
response.sendRedirect("../index.jsp");
}
%>
/Viewer/index.jsp:
Code:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@page
import="java.util.Iterator"
import="org.hibernate.Session"
import="org.hibernate.Transaction"
import="org.hibernate.Query"
import="entity.HibernateUtil"
import="entity.User"
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<table border="1">
<tr><td>Id</td><td>Name</td><td>Surname</td></tr>
<%
// Session sess = HibernateUtil.getSessionFactory().openSession();
Session sess = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = null;
try {
tx = sess.beginTransaction();
String SQL_QUERY = "from User s";
Query query = sess.createQuery(SQL_QUERY);
for (Iterator it = query.iterate(); it.hasNext();) {
User newuser = (User) it.next();
%>
<tr><td><%=newuser.getId()%></td><td><%=newuser.getName()%></td><td><%=newuser.getSurname()%></td></tr>
<%
}
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
// sess.close();
}
%>
</table>
</body>
</html>
hibernate.cfg.xml (common for both):
Code:
<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://localhost:3306/hibtest</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<mapping resource="entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
I used the Netbeans Hibernate wizard to automatically generate the POJOs, the hibernate mapping files, and the HibernateUtil file.