Sri*_*ari 5 java orm spring hibernate jpa
在我的代码中,我做了如下:
我对这种持久化方法的行为感到困惑.
请帮帮我.
代码如下:
//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)
当一个实体当前被管理(附加到一个会话)时,即使没有调用,对它的所有更新也会直接反映到底层存储persist().
在您的情况下,您加载您的实体,因此它在会话中.然后,即使您不调用persist()它,也会在数据库中更新事务提交.
persist()来自javadoc 的描述:
使实体实例受管理并持久化.
这意味着该方法在您的情况下不会执行任何操作,因为您的实体既是持久的又是托管的.
PS我在哪里说"会话",了解"实体经理"
| 归档时间: |
|
| 查看次数: |
2550 次 |
| 最近记录: |