我的实体类如下
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;
}
}
数据访问对象 - >
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为我的更新语句准备了以下查询 - &gt; 休眠: 更新InitialMedicalCheckUpG3设置IMCS_ID =?,REMARKS =?,STATUS = ?, UPDATED_BY = ?, UPDATED_ON =?其中EMP_ID =?
因此表格没有更新。
我使用服务类中的Dao-&gt; 包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);
}
}
答案 0 :(得分:1)
通过休眠的每个操作都需要与事务相关联。您需要确保更新操作是事务的一部分,并且已提交此工作单元。确保将此操作与事务关联并提交。
答案 1 :(得分:0)
在@Transactional
课程之前或每种方法之前使用InitialMedicalCheckUpDaoImpl
注释。然后,Spring将自动为此类中的所有方法添加事务特征。如果您希望拥有可以执行多个dao操作的事务,那么这就有缺点。
更好的设计是让另一个类(服务)调用dao中定义的方法。然后可以使用@Transactional
对该类进行注释。这将导致更好的事务定义,因为某些事务可以跨越多个数据库操作,如insert then update。
修改强>
我刚注意到你没有覆盖实体类中的equals和hashcode方法。 hibernate需要这些,以便在更新之前知道实体是否已经在数据库中,如果实体在某个时刻从会话中分离或在集合中使用。您可以使用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;
}
您可能需要查看this Question