Need help with Hibernate? Read this first:
i developed one example for CRUD opreations using struts and hibernate frmame work.but in this example i hard coded for one table in the database using this code how can develop all tables in the database.
in this example first main page will display in this page we can select table and CRUD operation. but in this code i hard coded for one table like EXAMPLETABLE . in this table two columns are there one id and name. but i want here to display all tables among thay one i want to selct one for CRUD operations. this can be possible using generalization cconcept in hibernate but iam not aware of that one plese help me how to develop app .
First open the Internet browser and type
http://localhost: 8080/ebase/main.jsp
You can get main’s in this The Reference table and Select the option to be perform with combo boxes you can Select whatever you want if select insert and table is Example Table. You will get that particular table insert form know we can select and submit
Here we developed one select table form and selectTableAction class using these class that particular forms forms will be displayed.
This we developed InsertTableForm, Insert Table Action .in from we developed setxxx and getxx functions for particular fields in action class all these fields are stored in to the table
In action class we written the execute method
Public ActionForward execute (ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
Throws Exception {
And creates the objects for action errors and Action forwards
ActionErrors errors = new ActionErrors (); // for errors
ActionForward forward = new ActionForward (); // return value
Next open the try block in this InsertActionForm is Typecasts into particular form like that
InsertTableForm stForm=(InsertTableForm) form;
String name = stForm.getName ();
We create the table objectlike that Table t=new Table ();
And set to the
t.setName(name);
in this class we create the object for ISelectTable ist=new SelectTableImpl ();
SelectTableImpl () we have one method call insert through this method we can insert these values.
public boolean insert(Table t){
try{
Session session=sessionFactory.openSession();
Transaction txt=session.beginTransaction();
session.save (t);
txt.commit();
session.close();
return true;
} catch(Exception e){
e.printStackTrace ();
return false;
}
In this method we open the created the session object
Transaction txt=session.beginTransaction(); Using this object we begin the transaction
And do the whatever operation s we required if the txs is successful txs is committed other wise rollback
Boolean flag=ist.insert (t);
if (flag)
return mapping.findForward ("insert");
Return mapping.findForward ("fail");
if it is insert return insert this will print insert successes other wise failure
Catch (Exception e){
e.printStackTrace ();
Errors.add ("error", new Action Error ("errorID"));
if any errors occurs exceptions will be thrown using above statements like that we developed using hibernate
if we select the operation is delete Form is displayed in this display from display all rows from the particular table. To get all rows we developed one method call getRows
public java.util.List getRows(){
Session session=sessionFactory.openSession();
Transaction txt=session.beginTransaction();
Query q=session.createQuery("from com.ebaseindia.valuesys.handling_data.tables.actions.Table");
java.util.List list=q.list();
txt.commit();
session.close();
System.out.println(((Table)list.get(0)).getNumber());
return list;
}
In this method we create the session object and begin the transaction
Query q=session.createQuery ("from com.ebaseindia.valuesys.handling_data.tables.actions.Table");
Using this query we can get the all the rows from data base .
All these are stored into the list.and again commit the txs and close the session.
In delete .jsp we can get these rows using these statements
<form action="delete.do">
<select name="rowID">
<%
java.util.List l=(java.util.List)session.getAttribute("tableFields");
for(int i=0;i<l.size();i++)
{
%>
<option>
<%
out.println( ((com.ebaseindia.valuesys.handling_data.tables.actions.Table)l.get(i)).getNumber());
%>
</option>
<%
}
%>
</select>
Using above statements we can get all these rows displaying in combo boxes
If user select the particular row and submit it will go the deletetableActionform and action class that action class delete the particular row through the delete method in the selecttableimpl class that delete method is like that
public boolean delete(Table t,int n){
Session session=sessionFactory.openSession ();
Transaction txt=session.beginTransaction();
session.load(t,new Integer(n));
session.delete(t);
txt.commit();
session.close();
return true;
}
session.load method is executed hibernate issues the select statement on db get the data from the database and stores the data into object
session.delete(t);
this statement will delete the particular row and TX will be committed
According to the txs the action class will display success or failure.
Like that update also work
update.jsp
<html>
<body>
<form action="getData.do">
<select name="rowID">
<%
java.util.List l=(java.util.List)session.getAttribute("tableFields");
for(int i=0;i<l.size();i++)
{
%>
<option>
<%
out.println( ((com.ebaseindia.valuesys.handling_data.tables.actions.Table)l.get(i)).getNumber());
%>
</option>
<%
}
%>
</select>
<input type="submit" value="update">
</form>
delete.jsp
<html>
<body>
<form action="delete.do">
<select name="rowID">
<%
java.util.List l=(java.util.List)session.getAttribute("tableFields");
for(int i=0;i<l.size();i++)
{
%>
<option>
<%
out.println( ((com.ebaseindia.valuesys.handling_data.tables.actions.Table)l.get(i)).getNumber());
%>
</option>
<%
}
%>
</select>
<input type="submit" value="delete">
</form>
<!--
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<html:html>
<body>
<html:form action="delete">
<html:select property="rowID">
<logic:iterate name="tableFields" id="tf">
<html:option value='<bean:write name="tf" property="name"/>'>
<bean:write name="tf" property="name"/>
</html:option>
</logic:iterate>
</html:select>
<html:submit/>
</html:form>
</body>
</html:html>
-->
display.jsp
<html>
<body>
<form action="update.do">
<%
java.util.List l=(java.util.List)session.getAttribute("tableFields");
for(int i=0;i<l.size();i++)
{
out.println("<input type=hidden name=number value="+request.getParameter("rowID")+">");
out.println("Number : "+ ((com.ebaseindia.valuesys.handling_data.tables.actions.Table)l.get(i)).getNumber());
out.println("</br>Name : <input type=\"text\" name=\"name\" value="+((com.ebaseindia.valuesys.handling_data.tables.actions.Table)l.get(i)).getName()+"></br>");
}
%>
<input type="submit" value="update">
</form>
</body>
</html>
insert.jsp
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<html:html>
<body>
<html:form action="insert">
Number :<html:text property="number"/>
Name :<html:text property="name"/>
<html:submit/>
</html:form>
</body>
</html:html>
strutsconfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<!-- Form Beans -->
<form-beans>
<form-bean
name="insertForm" type="com.ebaseindia.valuesys.handling_data.tables.forms.InsertTableForm">
</form-bean>
<form-bean
name="deleteForm" type="com.ebaseindia.valuesys.handling_data.tables.forms.DeleteTableForm">
</form-bean>
<form-bean
name="mainForm" type="com.ebaseindia.valuesys.handling_data.tables.forms.SelectTableForm">
</form-bean>
</form-beans>
<action-mappings>
<action
path="/main"
type="com.ebaseindia.valuesys.handling_data.tables.actions.SelectTableAction"
name="mainForm"
validate="false"
scope="session"
parameter="operation">
<forward name="insert" path="/Insert.jsp"/>
<forward name="delete" path="/Delete.jsp"/>
<forward name="update" path="/Update.jsp"/>
<forward name="fail" path="/fail.jsp"/>
</action>
<action
path="/insert"
type="com.ebaseindia.valuesys.handling_data.tables.actions.InsertTableAction"
name="insertForm"
validate="false"
scope="session">
<forward name="insert" path="/ISuccess.jsp"/>
<forward name="fail" path="/fail.jsp"/>
</action>
<action
path="/update"
type="com.ebaseindia.valuesys.handling_data.tables.actions.UpdateTableAction"
name="insertForm"
validate="false"
scope="session">
<forward name="update" path="/USuccess.jsp"/>
<forward name="fail" path="/fail.jsp"/>
</action>
<action
path="/delete"
type="com.ebaseindia.valuesys.handling_data.tables.actions.DeleteTableAction"
name="deleteForm"
validate="false"
scope="session">
<forward name="delete" path="/DSuccess.jsp"/>
<forward name="fail" path="/fail.jsp"/>
</action>
<action
path="/getData"
type="com.ebaseindia.valuesys.handling_data.tables.actions.SelectDataAction"
name="deleteForm"
validate="false"
scope="session">
<forward name="display" path="/Display.jsp"/>
<forward name="fail" path="/fail.jsp"/>
</action>
</action-mappings>
</struts-config>
iselecttable.java
package com.ebaseindia.valuesys.handling_data.tables;
import com.ebaseindia.valuesys.handling_data.tables.actions.Table;
public interface ISelectTable
{
public java.util.List getFields(String table,int number)throws Exception;
public boolean insert(Table table)throws Exception;
public java.util.List getRows()throws Exception;
public boolean delete(Table table,int n)throws Exception;
public boolean update(Table table,int number)throws Exception;
}
select tableimpl
package com.ebaseindia.valuesys.handling_data.tables.actions;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.ebaseindia.valuesys.handling_data.tables.ISelectTable;
public class SelectTableImpl implements ISelectTable
{
SessionFactory sessionFactory=null;
public SelectTableImpl(){
super();
init();
}
public void init(){
try{
sessionFactory = new Configuration().configure().buildSessionFactory();
}catch(Exception e){
e.printStackTrace();
}
}
public java.util.List getFields(String tableName,int number){
Session session=sessionFactory.openSession();
Transaction txt=session.beginTransaction();
Query q=session.createQuery("from com.ebaseindia.valuesys.handling_data.tables.actions.Table" +" tab where tab.number="+number);
java.util.List list=q.list();
txt.commit();
session.close();
System.out.println(((Table)list.get(0)).getNumber());
System.out.println(((Table)list.get(0)).getName());
return list;
}
public boolean insert(Table t){
try{
Session session=sessionFactory.openSession();
Transaction txt=session.beginTransaction();
session.save(t);
txt.commit();
session.close();
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
public java.util.List getRows(){
Session session=sessionFactory.openSession();
Transaction txt=session.beginTransaction();
Query q=session.createQuery("from com.ebaseindia.valuesys.handling_data.tables.actions.Table");
java.util.List list=q.list();
txt.commit();
session.close();
System.out.println(((Table)list.get(0)).getNumber());
return list;
}
public boolean delete(Table t,int n){
Session session=sessionFactory.openSession();
Transaction txt=session.beginTransaction();
session.load(t,new Integer(n));
session.delete(t);
txt.commit();
session.close();
return true;
}
}
table.java
ackage com.ebaseindia.valuesys.handling_data.tables.actions;
public class Table
{
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setNumber(int number){
this.number=number;
}
public int getNumber(){
return number;
}
private String name;
int number;
}
selecetableaction.java
package com.ebaseindia.valuesys.handling_data.tables.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.ebaseindia.valuesys.handling_data.tables.ISelectTable;
import com.ebaseindia.valuesys.handling_data.tables.forms.InsertTableForm;
import com.ebaseindia.valuesys.handling_data.tables.forms.SelectTableForm;
public class SelectTableAction extends DispatchAction
{
public ActionForward insert(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ActionErrors errors = new ActionErrors(); // for errors
ActionForward forward = new ActionForward(); // return value
String target="fail";
System.out.println("before try");
try{
SelectTableForm stForm=(SelectTableForm)form;
HttpSession session = request.getSession();
String tableName = stForm.getTableName();
session.setAttribute("tableName",tableName);
ISelectTable ist=new SelectTableImpl();
java.util.List tableFields=ist.getRows();
int max=0;
for(int i=0;i<tableFields.size();i++){
if( max<((Table)tableFields.get(i)).getNumber())
max=((Table)tableFields.get(i)).getNumber();
}
session.setAttribute("id",new Integer(max+1));
//session.setAttribute("tableFields",tableFields);
return mapping.findForward("insert");
}catch(Exception e){
System.out.println("in cathc");
e.printStackTrace();
errors.add("error",new ActionError("errorID"));
}
forward=new ActionForward(target);
return forward;
}
public ActionForward update(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ActionErrors errors = new ActionErrors(); // for errors
ActionForward forward = new ActionForward(); // return value
String target="fail";
System.out.println("before try");
try{
SelectTableForm stForm=(SelectTableForm)form;
HttpSession session = request.getSession();
String tableName = stForm.getTableName();
session.setAttribute("tableName",tableName);
ISelectTable ist=new SelectTableImpl();
java.util.List tableFields=ist.getRows();
session.setAttribute("tableFields",tableFields);
return mapping.findForward("update");
}catch(Exception e){
System.out.println("in cathc");
e.printStackTrace();
errors.add("error",new ActionError("errorID"));
}
forward=new ActionForward(target);
return forward;
}
public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ActionErrors errors = new ActionErrors(); // for errors
ActionForward forward = new ActionForward(); // return value
String target="fail";
System.out.println("before try");
try{
SelectTableForm stForm=(SelectTableForm)form;
HttpSession session = request.getSession();
String tableName = stForm.getTableName();
session.setAttribute("tableName",tableName);
ISelectTable ist=new SelectTableImpl();
java.util.List tableFields=ist.getRows();
session.setAttribute("tableFields",tableFields);
return mapping.findForward("delete");
}catch(Exception e){
System.out.println("in cathc");
e.printStackTrace();
errors.add("error",new ActionError("errorID"));
}
forward=new ActionForward(target);
return forward;
}
}
selecttableform
package com.ebaseindia.valuesys.handling_data.tables.forms;
import org.apache.struts.action.ActionForm;
public class SelectTableForm extends ActionForm
{
public void setTableName(String tableName){
this.tableName=tableName;
}
public String getTableName(){
return tableName;
}
public void setOperation(String operation){
this.operation=operation;
}
public String getOperation(){
return operation;
}
private String tableName,operation;
}
inserttableaction.java
package com.ebaseindia.valuesys.handling_data.tables.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.ebaseindia.valuesys.handling_data.tables.forms.InsertTableForm;
import com.ebaseindia.valuesys.handling_data.tables.forms.SelectTableForm;
import com.ebaseindia.valuesys.handling_data.tables.ISelectTable;
public class InsertTableAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ActionErrors errors = new ActionErrors(); // for errors
ActionForward forward = new ActionForward(); // return value
String target="error";
try{
InsertTableForm stForm=(InsertTableForm)form;
String name = stForm.getName();
// String number=stForm.getNumber();
Table t=new Table();
// t.setNumber(number);
t.setName(name);
ISelectTable ist=new SelectTableImpl();
boolean flag=ist.insert(t);
if(flag)
return mapping.findForward("insert");
return mapping.findForward("fail");
}catch(Exception e){
e.printStackTrace();
errors.add("error",new ActionError("errorID"));
}
forward=new ActionForward(target);
return forward;
}
}
insert table form.java
package com.ebaseindia.valuesys.handling_data.tables.forms;
import org.apache.struts.action.ActionForm;
public class InsertTableForm extends ActionForm
{
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setNumber(int number){
this.number=number;
}
public int getNumber(){
return number;
}
private String name;
private int number;
}
updatetableaction.java
package com.ebaseindia.valuesys.handling_data.tables.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.ebaseindia.valuesys.handling_data.tables.forms.InsertTableForm;
import com.ebaseindia.valuesys.handling_data.tables.forms.SelectTableForm;
import com.ebaseindia.valuesys.handling_data.tables.ISelectTable;
public class UpdateTableAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ActionErrors errors = new ActionErrors(); // for errors
ActionForward forward = new ActionForward(); // return value
String target="error";
try{
InsertTableForm stForm=(InsertTableForm)form;
String name = stForm.getName();
int number=stForm.getNumber();
Table t=new Table();
t.setNumber(number);
t.setName(name);
ISelectTable ist=new SelectTableImpl();
boolean flag=ist.update(t,number);
if(flag)
return mapping.findForward("update");
return mapping.findForward("fail");
}catch(Exception e){
e.printStackTrace();
errors.add("error",new ActionError("errorID"));
}
forward=new ActionForward(target);
return forward;
}
}
detetetableAction.java
package com.ebaseindia.valuesys.handling_data.tables.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.ebaseindia.valuesys.handling_data.tables.forms.DeleteTableForm;
import com.ebaseindia.valuesys.handling_data.tables.ISelectTable;
public class DeleteTableAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ActionErrors errors = new ActionErrors(); // for errors
ActionForward forward = new ActionForward(); // return value
String target="error";
try{
DeleteTableForm stForm=(DeleteTableForm)form;
int number = stForm.getRowID();
Table t=new Table();
ISelectTable ist=new SelectTableImpl();
boolean flag=ist.delete(t,number);
if(flag)
return mapping.findForward("delete");
return mapping.findForward("fail");
}catch(Exception e){
e.printStackTrace();
errors.add("error",new ActionError("errorID"));
}
forward=new ActionForward(target);
return forward;
}
}
delteform
package com.ebaseindia.valuesys.handling_data.tables.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.ebaseindia.valuesys.handling_data.tables.forms.DeleteTableForm;
import com.ebaseindia.valuesys.handling_data.tables.ISelectTable;
public class DeleteTableAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ActionErrors errors = new ActionErrors(); // for errors
ActionForward forward = new ActionForward(); // return value
String target="error";
try{
DeleteTableForm stForm=(DeleteTableForm)form;
int number = stForm.getRowID();
Table t=new Table();
ISelectTable ist=new SelectTableImpl();
boolean flag=ist.delete(t,number);
if(flag)
return mapping.findForward("delete");
return mapping.findForward("fail");
}catch(Exception e){
e.printStackTrace();
errors.add("error",new ActionError("errorID"));
}
forward=new ActionForward(target);
return forward;
}
}
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:oracle</property>
<property name="connection.user">scott</property>
<property name="connection.password">tiger</property>
<!-- Mapping files -->
<mapping resource="ExampleTable.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Example.hbm.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:oracle</property>
<property name="connection.user">scott</property>
<property name="connection.password">tiger</property>
<!-- Mapping files -->
<mapping resource="ExampleTable.hbm.xml"/>
</session-factory>
</hibernate-configuration>
selectDataAction.java
package com.ebaseindia.valuesys.handling_data.tables.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.ebaseindia.valuesys.handling_data.tables.forms.DeleteTableForm;
import com.ebaseindia.valuesys.handling_data.tables.ISelectTable;
public class SelectDataAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ActionErrors errors = new ActionErrors(); // for errors
ActionForward forward = new ActionForward(); // return value
String target="error";
try{
DeleteTableForm stForm=(DeleteTableForm)form;
HttpSession session = request.getSession();
int number = stForm.getRowID();
Table t=new Table();
ISelectTable ist=new SelectTableImpl();
java.util.List tableFields=ist.getFields((String)session.getAttribute("tableName"),number);
session.setAttribute("tableFields",tableFields);
return mapping.findForward("display");
}catch(Exception e){
e.printStackTrace();
errors.add("error",new ActionError("errorID"));
}
forward=new ActionForward(target);
return forward;
}
}
this is my code anyone can help me
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.0
Mapping documents:
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:any
Name and version of the database you are using:
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
[code][/code]