Reputation: 1
I am not able to edit a table using Struts 2 and Hibernate. In Apache console I can see that the update query is executed, but from a table it's just deleting the data, and while trying to insert it, getting inserted in a new row not in the same row. Can anyone please help what is wrong in this code:
DAO for edit:
public static boolean update(trainee p)
{
Boolean a=false;
Session session=new Configuration().configure("hibernate.cfg.xml").buildSessionFactory().openSession();
Transaction tp=session.beginTransaction();
trainee u=(trainee) session.get(trainee.class, p.getId());
if(u!=null)
{
u.setAddress(p.getAddress());
u.setPhone(p.getPhone());
u.setAge(p.getAge());
u.setTname(p.getTname());
u.setGender(p.getGender());
u.setTechnology(p.getTechnology());
u.setEmail(p.getEmail());
session.update(u);
}
tp.commit();
System.out.println("Command successfully executed....");
session.close();
if(tp != null){
a=true;
}
return a;
}
Action class:
public String update()
{
String x="input";
trainee u=new trainee();
u.setId(id);
u.setAddress(address);
u.setPhone(phone);
u.setAge(age);
u.setTname(tname);
u.setGender(gender);
u.setTechnology(technology);
u.setEmail(email);
if(RegisterDao.update(u))
{
x="success";
}
return x;
}
struts.xml:
<action name="update" class="com.ilp.action.taineeAction" method="update">
<result name="success" type="redirect">TraineeRegistration.jsp</result>
<result name="input">ViewTrainees.jsp</result>
</action>
TraineeRegistration.jsp:
<s:form action="traineeReg">
<s:textfield label="TraineeName" name="tname"></s:textfield>
<s:textfield label="Email Id" name="email"></s:textfield>
<s:radio name="gender" list="{'Male','Female'}"></s:radio>
<s:textfield label="Age" name="age"></s:textfield>
<s:textfield label="Phone Number" name="phone"></s:textfield>
<s:textarea label="Address" name="address"></s:textarea>
<s:select name="technology" list="{'Java','J2ee','Dot Net','Oracle'}" headerKey="" headerValue="Select" label="SelectTechnology" />
<s:submit/>
</s:form>
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="hbm2ddl.auto">update</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/javadb</property>
<property name="connection.username">root</property>
<property name="connection.password">cis@123</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<mapping class="com.ilp.bean.trainee"/>
</session-factory>
viewtrainee.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>List Of Trainees Registered</title>
</head>
<body>
<table border=1px>
<tr>
<th>Trainee Name</th>
<th>Email</th>
<th>Gender</th>
<th>Age</th>
<th>Ph No</th>
<th>Address</th>
<th>Technology</th>
<th>Edit</th>
<th>Delete</th>
<s:iterator value="contactList">
<tr>
<td><s:property value="tname" /></td>
<td><s:property value="email" /></td>
<td><s:property value="gender" /></td>
<td><s:property value="age" /></td>
<td><s:property value="phone" /></td>
<td><s:property value="address" /></td>
<td><s:property value="technology" /></td>
<td><a href="update?id=<s:property value="id"/>">edit</a></td>
<td><a href="delete?id=<s:property value="id"/>">delete</a></td>
</tr>
</s:iterator>
</table>
<a href="TraineeRegistration.jsp">Add Trainee</a>
</body>
</html>
Upvotes: 0
Views: 1671
Reputation: 2203
Okay, Your problem is when you click on update link on success you are opening TraineeRegistration.jsp
and in this jsp you have defined <s:form action="traineeReg">
so after filling form when you click on submit button it will fire new insert action associated with action="traineeReg"
and it will insert data as new row.
What you can do is you can create one more action EDIT
and in that action write code to fetch data of selected id and than pass that data to jsp and fill form with existing values than on submit write code to update data: Following code will help you to get particular data of ID:
EditAction.java
public String edit()
{
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session s = sf.openSession();
Query q = s.getNamedQuery("findbyid").setInteger("id", getId());
al = (ArrayList<User>)q.list();
return"success";
}
To use namedquery
you need to add following code into your name.hbm.xml
after end of class tag </class>
.
<query name="findbyid">
<![CDATA[from User u where u.id = :id]]>
</query>
Than pass this list to jsp and iterate it
edit.jsp
<s:form action="update">
<s:iterator id="lis" value="al">
<s:hidden name="id" value="%{id}"></s:hidden>
<s:textfield name="name" label="Name" value="%{name}"></s:textfield>
<s:textfield name="city" label="City" value="%{city}"></s:textfield>
<s:textfield name="pin" label="Pincode" value="%{pin}"></s:textfield>
<s:submit value="Save"/>
</s:iterator>
</s:form>
And than on this update action fire update code which you have already wrote. You also need to add edit action entry in struts.xml
file:
<action name="edit" class="com.EditAction" method="edit">
<result name="success">/edit.jsp</result>
</action>
You can also use TraineeRegistration.jsp
instead of creating new jsp edit.jsp
but sake of simplicity create new jsp.
Hope this helps!!!
Upvotes: 0
Reputation: 1
You didn't set the id
property in the action. Once property is set with the value of the object you get in the hibernate session you can bind the hidden field to this property using OGNL.
<s:form action="traineeReg">
<s:hidden name="id" value="%{id}"/>
<s:textfield label="TraineeName" name="tname"></s:textfield>
<s:textfield label="Email Id" name="email"></s:textfield>
<s:radio name="gender" list="{'Male','Female'}"></s:radio>
<s:textfield label="Age" name="age"></s:textfield>
<s:textfield label="Phone Number" name="phone"></s:textfield>
<s:textarea label="Address" name="address"></s:textarea>
<s:select name="technology" list="{'Java','J2ee','Dot Net','Oracle'}" headerKey="" headerValue="Select" label="SelectTechnology" />
<s:submit/>
</s:form>
When you call u.setId(id);
the value should be already there.
You should also modify the code to able to insert a new record.
if (p.getId() != null) {
trainee u=(trainee) session.get(trainee.class, p.getId());
u.setAddress(p.getAddress());
u.setPhone(p.getPhone());
u.setAge(p.getAge());
u.setTname(p.getTname());
u.setGender(p.getGender());
u.setTechnology(p.getTechnology());
u.setEmail(p.getEmail());
session.update(u);
} else session.save(p);
Upvotes: 1