删除态:抛异常org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.ObjectDeletedException: deleted instance passed to merge: [com.example.demo.entity.User#<null>]...
游离态:提交到数据库,进行更新或插入,返回一个新的托管态的对象
合并(merge)删除态和游离态验证过程如下:
1.删除态
代码
12
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
* 持久化删除态的对象
*@param id
* @return
*/
@RequestMapping("/mergeRemove/{id}")
public String mergeRemove(@PathVariable("id")Long id){
try {
User user = userDao.findById(id);
userDao.mergeRemove(user);
}catch (Exception e){
logger.error("合并(merge)一个删除态的对象!",e);
return REPONSE_ERR;
}
return REPONSE_SUCCESS;
}
--------------------------------------------------------------------
/**
* 合并删除态的对象
*
* @param user
*/
@Override
public void mergeRemove(User user) {
remove(user);
merge(user);
}
步骤
:8080/user/list,获取`id`
:8080/user/mergeRemove/{id}
结果:返回ERROR抛异常!
12
3
4
5
6
7
8
9
2019-01-26 00:23:01.187 INFO 10136 --- [nio-8080-exec-3] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select user0_.id as id1_0_, user0_.name as name2_0_, user0_.number as number3_0_ from t_user user0_ where user0_.id=?
2019-01-26 00:23:01.322 ERROR 10136 --- [nio-8080-exec-3] c.e.demo.controller.UserController : 合并(merge)一个删除态的对象!
org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.ObjectDeletedException: deleted instance passed to merge: [com.example.demo.entity.User#<null>]; nested exception is java.lang.IllegalArgumentException: org.hibernate.ObjectDeletedException: deleted instance passed to merge: [com.example.demo.entity.User#<null>]
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:373) ~[spring-orm-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:255) ~[spring-orm-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527) ~[spring-orm-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61) ~[spring-tx-5.1.4.RELEASE.jar:5.1.4.RELEASE]
2.游离态