小编Sri*_*ari的帖子

Spring + JPA + Hibernate:persist正在令人惊讶地更新实体.请详细说明

在我的代码中,我做了如下:

  • 询问课程实体
  • 用给定的课程数据填充它.
  • courseDao.update(entity),它在内部调用persist(entity)方法.
  • 令人惊讶的是,数据已成功更新.

我对这种持久化方法的行为感到困惑.

请帮帮我.

代码如下:

//My Service......
@Service("myService")
@Transactional
public class MyServiceImpl implements MyService {

  @Transactional(rollbackFor = { Throwable.class })
  public void updateCourse(final Course course) throws MyServiceException {
    ------
    ------
    CourseEntity courseEntity = courseDao.findById(course.getId());
    populateCourseEntity(courseEntity, course);

    courseDao.update(courseEntity);
  }
}

//CourseDao.....

public class CourseDaoImpl implements CourseDao {
   --------
   public void update(final T entity) throws MyDaoException {
        if (entity != null) {
            this.entityManager.persist(entity);
        }
        else {
            String errMsg = "Object to be updated cannot be null.";
            throw new MyDaoException(errMsg); …
Run Code Online (Sandbox Code Playgroud)

java orm spring hibernate jpa

5
推荐指数
1
解决办法
2550
查看次数

标签 统计

hibernate ×1

java ×1

jpa ×1

orm ×1

spring ×1