HIbernate saveorUpdate() inserting new records while updating
<p>I am using struts2 and hibernate Integration by following this link <a href="http://www.tutorials4u.net/struts2-tutorial/struts2_crud_example.html" rel="nofollow noreferrer">http://www.tutorials4u.net/struts2-tutorial/struts2_crud_example.html</a> and trying to update the records but instead of updating it is inserting new records , I have seen all the hibernate update questions before posting this post but none of them worked for me so Please I am requesting to read my question before marking it as duplicate, as I had tried a lot from the already posted questions and answers...</p>

<p><strong>AddStudentAction.java</strong></p>

<pre><code>public class AddStudentAction extends ActionSupport implements
        ModelDriven&lt;Student&gt; {

    public AddStudentAction() {
        // TODO Auto-generated constructor stub
    }

    Student student = new Student();

    private String firstName;

    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstNameSearch(String firstName) {
        this.firstName = firstName;
    }

    List&lt;Student&gt; students = new ArrayList&lt;Student&gt;();

    List&lt;Student&gt; studentFirstNames = new ArrayList&lt;Student&gt;();

    public List&lt;Student&gt; getStudentFirstNames() {
        return studentFirstNames;
    }

    public void setStudentFirstNames(List&lt;Student&gt; studentFirstNames) {
        this.studentFirstNames = studentFirstNames;
    }

    StudentDAO dao = new StudentDAO();

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }

    public List&lt;Student&gt; getStudents() {
        return students;
    }

    public void setStudents(List&lt;Student&gt; students) {
        this.students = students;
    }

    public StudentDAO getDao() {
        return dao;
    }

    public void setDao(StudentDAO dao) {
        this.dao = dao;
    }

    @Override
    public Student getModel() {
        // TODO Auto-generated method stub
        return student;
    }
@Override
public String execute() {
    // TODO Auto-generated method stub
    dao.addStudent(student);
    String f = student.getFirstName();
    String l = student.getLastName();
    int m = student.getMarks();
    System.out.println(f + l + m + "Inside execute method");
    return "success";
}

public String updateStudent() {

    dao.addStudent(student);
    return "success";
}

public String listStudents() {
    students = dao.getStudents();
    return "success";
}
public String editStudent() {
    HttpServletRequest request = (HttpServletRequest) ActionContext
            .getContext().get(ServletActionContext.HTTP_REQUEST);
    student = dao.listStudentById(Integer.parseInt((request
            .getParameter("id"))));

    student.setId(Integer.parseInt((request.getParameter("id"))));

    System.out.println(request.getParameter("id") + "id in editStudent");
    dao.updateStudent(student);

    return SUCCESS;
}
</code></pre>

<p><strong>StudentDAO.java</strong></p>

<pre><code>public class StudentDAO {
@SessionTarget
Session session;

@TransactionTarget
Transaction transaction;
public void addStudent(Student student) {
    try {
        session.saveOrUpdate(student);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void updateStudent(Student student) {
    try {

        session.saveOrUpdate(student);
        session.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
public Student listStudentById(int Id) {
    Student student = null;
    try {
        student = (Student) session.get(Student.class, Id);
    } catch (Exception e) {

    }
    return student;
}
</code></pre>

<p>}</p>

<p><strong>Student.java</strong></p>

<pre><code>    package com.struts2hibernatepagination.hibernate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "student")
public class Student {

    @Id
    @GeneratedValue
    @Column(name = "id")
    private int id;
    @Column(name = "last_name")
    private String lastName;
    @Column(name = "first_name")
    private String firstName;
    private int marks;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public int getMarks() {
        return marks;
    }

    public void setMarks(int marks) {
        this.marks = marks;
    }

}
</code></pre>

<p><strong>struts.xml</strong></p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
</code></pre>

<p></p>

<p>
    </p>

<pre><code>&lt;package name="default" extends="hibernate-default"&gt;
&lt;action name="addStudent" method="execute"
        class="com.struts2hibernatepagination.action.AddStudentAction"&gt;
        &lt;interceptor-ref name="defaultStackHibernateStrutsValidation"&gt;
            &lt;param name="validation.excludeMethods"&gt;listStudents&lt;/param&gt;
            &lt;param name="validation.excludeMethods"&gt;fetchStudentList&lt;/param&gt;
        &lt;/interceptor-ref&gt;
        &lt;result name="success" type="redirect"&gt;
            listStudents
        &lt;/result&gt;
        &lt;result name="input"&gt;/student.jsp&lt;/result&gt;
    &lt;/action&gt;
&lt;action name="editStudent"
        class="com.struts2hibernatepagination.action.AddStudentAction"
        method="editStudent"&gt;
        &lt;interceptor-ref name="basicStackHibernate" /&gt;
        &lt;result name="success"&gt;/student.jsp&lt;/result&gt;
    &lt;/action&gt;
&lt;/package&gt;
</code></pre>

<p></p>

<p><strong>hibernate.cfg.xml</strong></p>

<pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
&lt;hibernate-configuration&gt;
    &lt;session-factory&gt;
        &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
        &lt;property name="hibernate.connection.password"&gt;admin12345&lt;/property&gt;
        &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://192.168.1.3:3306/nupur&lt;/property&gt;
        &lt;property name="hibernate.connection.username"&gt;root&lt;/property&gt;
        &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
        &lt;property name="show_sql"&gt;true&lt;/property&gt;
        &lt;property name="hibernate.hbm2ddl.auto"&gt;update&lt;/property&gt;
        &lt;mapping class="com.struts2hibernatepagination.hibernate.Student" /&gt;
    &lt;/session-factory&gt;
&lt;/hibernate-configuration&gt;
</code></pre>

<p><strong>student.jsp</strong></p>

<pre><code>   &lt;%@ page contentType="text/html; charset=UTF-8"%&gt;
&lt;%@ taglib prefix="s" uri="/struts-tags"%&gt;

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Hello World&lt;/title&gt;
&lt;s:head /&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;a href="searchStudent.jsp"&gt;Search&lt;/a&gt;
    &lt;s:form action="addStudent"&gt;
        &lt;s:actionerror /&gt;
        &lt;s:textfield name="firstName" label="First Name" /&gt;
        &lt;s:textfield name="lastName" label="Last Name" /&gt;
        &lt;s:textfield name="marks" label="Marks" /&gt;
        &lt;s:submit value="Save" /&gt;
        &lt;hr /&gt;
        &lt;table border="2"&gt;
            &lt;tr bgcolor="cyan"&gt;
                &lt;th&gt;&lt;u&gt;First Name&lt;/u&gt;&lt;/th&gt;
                &lt;th&gt;&lt;u&gt;Last Name&lt;/u&gt;&lt;/th&gt;
                &lt;th&gt;&lt;u&gt;Marks&lt;/u&gt;&lt;/th&gt;
                &lt;th&gt;&lt;u&gt;Edit&lt;/u&gt;&lt;/th&gt;
                &lt;th&gt;&lt;u&gt;Delete&lt;/u&gt;&lt;/th&gt;
            &lt;/tr&gt;
            &lt;s:iterator value="students"&gt;
                &lt;tr&gt;
                    &lt;td&gt;&lt;s:property value="firstName" /&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;s:property value="lastName" /&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;s:property value="marks" /&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;s:url id="editURL" action="editStudent"&gt;
                            &lt;s:param name="id" value="%{id}"&gt;&lt;/s:param&gt;
                        &lt;/s:url&gt; &lt;s:a href="%{editURL}"&gt;Edit&lt;/s:a&gt;&lt;/td&gt;
                    &lt;td&gt;&lt;s:url id="deleteURL" action="deleteStudent"&gt;
                            &lt;s:param name="id" value="%{id}"&gt;&lt;/s:param&gt;
                        &lt;/s:url&gt; &lt;s:a href="%{deleteURL}"&gt;Delete&lt;/s:a&gt;&lt;/td&gt;
                &lt;/tr&gt;
            &lt;/s:iterator&gt;
        &lt;/table&gt;

    &lt;/s:form&gt;

    &lt;s:form action="fetchStudentList"&gt;
        &lt;s:submit value="Show"&gt;&lt;/s:submit&gt;

    &lt;/s:form&gt;


&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>Please help Me... As I tried all ways whether it is of using session.flush() or deleting @GeneratedValue annotation</p>

<p><strong>Id not coming in addStudent()</strong></p>

<pre><code>public String editStudent() {
HttpServletRequest request = (HttpServletRequest) ActionContext
                        .getContext().get(ServletActionContext.HTTP_REQUEST);
                dao.listStudentById(Integer.parseInt((request.getParameter("id"))));                return SUCCESS;
            } //AddStudetnAction class method

                                                                         public void addStudent(Student student) {
                try {  
                System.out.println(student.getId() + "In DAO addStudent");// returning null
                 session.saveOrUpdate(student);

                } catch (Exception e) {
                    e.printStackTrace();
                }

  //StudentDAO class method                                                                                       
</code></pre>

<p>The <code>addStudent()</code> of <code>StudentDAO</code>is getting <code>null</code> value for <code>id</code>, how can I set the id for <code>addStudent()</code> so that it calls update method instead of save      </p>