Dumb Fella
Dumb Fella

Reputation: 85

Unable to update table using hibernate spring mvc

My entity class is as follows

package com.ibs.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "InitialMedicalCheckUpG3")
public class InitialMedicalCheckUpEntity {

    private int imcsId;
    private String empId, status, updatedBy, remarks;
    Date updatedOn;

    @Column(name = "IMCS_ID")
    public int getImcsId() {
        return imcsId;
    }

    public void setImcsId(int imcsId) {
        this.imcsId = imcsId;
    }

    @Id
    @Column(name = "EMP_ID")
    @GeneratedValue(generator = "increment", strategy = GenerationType.AUTO)
    @GenericGenerator(name = "increment", strategy = "increment")
    public String getEmpId() {
        return empId;
    }

    public void setEmpId(String empId) {
        this.empId = empId;
    }

    @Column(name = "STATUS")
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Column(name = "UPDATED_BY")
    public String getUpdatedBy() {
        return updatedBy;
    }

    public void setUpdatedBy(String updatedBy) {
        this.updatedBy = updatedBy;
    }

    @Column(name = "REMARKS")
    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "UPDATED_ON", insertable = false)
    public Date getUpdatedOn() {
        return updatedOn;
    }

    public void setUpdatedOn(Date updatedOn) {
        this.updatedOn = updatedOn;
    }

}

Data access object ->

package com.ibs.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ibs.entity.InitialMedicalCheckUpEntity;

@Repository
public class InitialMedicalCheckUpDaoImpl implements InitialMedicalCheckUpDao {

    @Autowired
    SessionFactory sessionFactory;

    @Override
    public List<InitialMedicalCheckUpEntity> getConfirmedList() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<InitialMedicalCheckUpEntity> getNonConfirmedList() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void update(InitialMedicalCheckUpEntity e) {
        // TODO Auto-generated method stub
        System.out.println(e.getStatus());
        sessionFactory.getCurrentSession().update(e);
    }

}

Hibernate prepared the following query for my update statement-> Hibernate: update InitialMedicalCheckUpG3 set IMCS_ID=?, REMARKS=?,STATUS=?,UPDATED_BY=?, UPDATED_ON=? where EMP_ID=?

So the table is not getting updated.

I use Dao from a service class-> package com.ibs.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ibs.dao.InitialMedicalCheckUpDao;
import com.ibs.entity.InitialMedicalCheckUpEntity;

@Service
public class InitialMedicalCheckUpServiceImpl implements
    InitialMedicalCheckUpService {

    @Autowired
    InitialMedicalCheckUpDao dao;

    @Override
    public List<InitialMedicalCheckUpEntity> getConfirmedList() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<InitialMedicalCheckUpEntity> getNonConfirmedList() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    @Transactional
    public void update(InitialMedicalCheckUpEntity e) {
        // TODO Auto-generated method stub
        dao.update(e);

    }

}

Upvotes: 1

Views: 1119

Answers (2)

Use the @Transactional annotation before the InitialMedicalCheckUpDaoImpl class, or before every method. Spring will then automatically add transactional characteristics for all methods within this class. This has the disadvantage when you would like to have transactions that can do more than one dao action.

A better design would be to have another class (a service) that calls the methods defined in the dao. This class can then be annotated with the @Transactional. This will result in better transaction definition because some transactions can then span more than one database operation, like insert then update.

EDIT

I just noticed that you have not overriden the equals and hashcode methods in your entity class. These are needed by hibernate to know if the entity is already in the database before updating, if the entity was detached from the session at some point or is used in a set. You can use the id field for this.

@Override
public boolean equals(Object o) {
    if (o == this) {
        return true;
    }
    if (o == null) {
        return false;
    }
    if (!(o instanceof InitialMedicalCheckUpEntity)) {
        return false;
    }
    return this.getEmpId()==o.getEmpId();
}

@Override
public int hashCode() {
    return empId;
}

You may want to take a look at this Question

Upvotes: 0

ajay.patel
ajay.patel

Reputation: 1967

Every operation through hibernate needs to be associated with a transaction. You need to make sure that your update operation is a part of a transaction and this unit of work is committed. Make sure that you are associating this operation with a transaction and commit it.

Upvotes: 1

Related Questions