Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate updating the record upon calling the setter methods of the bean class

I am new with this language. I have some rows in employee table and the bean class is Employee. I have fetched one record

Employee employee=this.employeeDaoImpl.getEmployeeObject(employeeId);

This is the CONTROLLER

@Transactional
@RequestMapping(value="/revise_payroll")
public String revise_payroll(HttpServletRequest req,HttpServletResponse resp, Model model,RedirectAttributes redirect){
    System.out.println("in revise payroll");
    String employeeId=req.getParameter("employeeId");
    System.out.println("E_ID for revise:"+employeeId);

    List<IncrementDecrementPayrollTemp> tempPayrollList=this.employeeDaoImpl.getTemporaryPayroll(employeeId);
    //get employee object from session
    List<Employee> empList=this.employeeDaoImpl.getCurrentCTC(employeeId);
    System.out.println("empList has: "+empList.toString());

    Employee employee=this.employeeDaoImpl.getCurrentCTCasObject(employeeId);
    System.out.println(("in controller employee hashcode: "+employee.toString()));


    int count=0;
// this will run for only one time

    for(IncrementDecrementPayrollTemp tempPayroll:tempPayrollList){
        employee.setCtc(tempPayroll.getCtct());
        employee.setBasicMonthly(tempPayroll.getBasicMonthlyt());
        employee.setBasicAnnual(tempPayroll.getBasicAnnualt());
        employee.setDaMonthly(tempPayroll.getDaMonthlyt());
        employee.setDaAnnual(tempPayroll.getDaAnnualt());
        employee.setHouserentMonthly(tempPayroll.getHouserentMonthlyt());
        employee.setHouserentAnnual(tempPayroll.getHouserentAnnualt());
        employee.setConveyanceMonthly(tempPayroll.getConveyanceMonthlyt());
        employee.setConveyanceAnnual(tempPayroll.getConveyanceAnnualt());
        employee.setMedicalMonthly(tempPayroll.getMedicalMonthlyt());
        employee.setMedicalAnnual(tempPayroll.getMedicalAnnualt());
        employee.setSpecialMonthly(tempPayroll.getSpecialMonthlyt());
        employee.setSpecialAnnual(tempPayroll.getSpecialAnnualt());
        employee.setPfMonthly(tempPayroll.getPfMonthlyt());
        employee.setPfAnnual(tempPayroll.getPfAnnualt());
        employee.setEsiMonthly(tempPayroll.getEsiMonthlyt());
        employee.setEsiAnnual(tempPayroll.getEsiAnnualt());
        employee.setMonthlySalary(tempPayroll.getMonthlySalaryt());
    }

    return new ModelAndView ("IncrementDecrementStatus");
}

Now, when I am just calling the setter methods on employee object, its updating the sql records, in the controller itself. I am not yet in DAO layer using session.save or any update function.

This is DAO Layer

Session session=this.sessionFactory.getCurrentSession();
    String p=employeeId.trim();
    String hql="From Employee e where e.employeeId=?";
    Query query=session.createQuery(hql);
    query.setString(0, p);  
    List<Employee> employeeList=(List<Employee>)query.list();
    System.out.println("dao list has "+employeeList.toString());
// to update the existing records
    for(Employee emp:employeeList){
        int id=emp.getId();
        System.out.println("id got: "+id);
        Employee empl=(Employee) session.get(Employee.class, id);

        String version=empl.getVersion();
        System.out.println("version is: "+version);
        int intVersion=Integer.valueOf(version);
        intVersion=intVersion+1;
        version=String.valueOf(intVersion);
        empl.setVersion(version);
        System.out.println("version and ctc in empl is: "+empl.getVersion()+" , "+empl.getCtc());
        System.out.println("hash code in loop: "+empl.toString());
        session.update(empl);

    }
// this is to save new record
  Integer i=(Integer)session.save(sessionEmployee);
    System.out.println("save returned: "+i.toString());
}

Things I want to achieve is, I want to update the existing records already in sql table and then save the employee object with some new set of values as a new record. Please suggest me where I am wrong. Thank you!

like image 518
Subhasish Bhattacharjee Avatar asked Dec 18 '25 17:12

Subhasish Bhattacharjee


1 Answers

Let me tell you the lifecycle states of an entity which can make you more clear about this behaviour.

An entity can exist in three states - Transient, Persistent and Detached.

Transient - When you create an object but do not associate it with Hibernate session, then it is in Transient state. Any modifications to such object using setter methods doesn't reflect the change in the database.

Persistent - Here the object is attached to the Hibernate session. So now the Hibernate session manages this object. Any changes made to this object gets reflected in the database. Because Hibernate designed it in such way that, if any modifications is made to a Persistent object, it automatically gets updated in the database, when the session is flushed. (This is Hibernate's capability).

Detached - This state is similar to Transient. The only difference is that an object in detached state was previously in the session(i.e. in persistent state ). But now this is out of the session, because of either closing of the session or calling the evict(Object) method of session.

So coming to your case, once you have loaded the object from database, the object is associated with the session, and thus is in persistent state. As this object is in Persistent state, and you made changes to a Persistent object, the changes are reflected back to database.

Coming to your requirement, (Dividing the problem into parts)

  • You want to get an existing record from the table - Use Employee empl=(Employee) session.get(Employee.class, id);

  • Now you want to make changes to this object but not to the database. So use session.evict(empl); to bring the object to detached state. Then after this, you can make modifications to the detached empl object.

  • Now you want to save this set of new values as a new record. So make sure you change the "id" property of the empl object, as you can't violate unique constraint of the id value. You can't have two records with the same id value in the table.

  • Don't forget to commit the transaction.

like image 52
Manish Avatar answered Dec 20 '25 08:12

Manish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!