简体   繁体   English

无法使用休眠的Spring MVC更新表

[英]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=? Hibernate为我的更新语句-> Hibernate准备了以下查询:update InitialMedicalCheckUpG3 set IMCS_ID = ?, REMARKS = ?, STATUS = ?, UPDATED_BY = ?, UPDATED_ON =? where EMP_ID=? 其中EMP_ID =?

So the table is not getting updated. 因此该表未更新。

I use Dao from a service class-> package com.ibs.service; 我从服务类->包com.ibs.service使用Dao;

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);

    }

}

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. 确保您将此操作与事务关联并提交。

Use the @Transactional annotation before the InitialMedicalCheckUpDaoImpl class, or before every method. InitialMedicalCheckUpDaoImpl类之前或每个方法之前使用@Transactional批注。 Spring will then automatically add transactional characteristics for all methods within this class. 然后,Spring将自动为此类中的所有方法添加事务特性。 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. 更好的设计是拥有另一个类(服务),该类调用dao中定义的方法。 This class can then be annotated with the @Transactional . 然后可以使用@Transactional注释此类。 This will result in better transaction definition because some transactions can then span more than one database operation, like insert then update. 这将导致更好的事务定义,因为某些事务然后可以跨越一个以上的数据库操作,例如insert然后更新。

EDIT 编辑

I just noticed that you have not overriden the equals and hashcode methods in your entity class. 我只是注意到您尚未在实体类中覆盖equals和hashcode方法。 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. 您可以为此使用id字段。

@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 您可能想看看这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM