I am not able to edit table using struts2 hibernate. In Apache console I can see that the update query is executed but from table its 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>
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.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.