---
title: "Entity must be managed to call remove"
author: "duyunfengsolo"
date: "2009-12-22T12:07:22Z"
tags: []
categories: ["企业架构"]
slug: "entity-must-be-managed-to-call-remove"
draft: false
lastmod: "2009-12-22T15:30:53Z"
id: 553712
url: "https://solodu.iteye.com/blog/553712"
---

```
struts2.0和spring2.5还有jpa在整合的时候会报告这样的异常：
严重: Servlet.service() for servlet default threw exception
java.lang.IllegalArgumentException: Entity must be managed to call remove: com.ctit.pojo.Student@1cd2197, try merging the detached and try the remove again.
```

 我在网上找了很长时间，但是没有找到解决办法。现在被我解决了。

 

有2种原因造成的：

```
原因1. 在dao层中：
public void deleteStudent(Student student) {
   Student mergeStudent = this.getJpaTemplate().merge(student);
   getJpaTemplate().remove(mergeStudent);
}
一定要先merge,然后才能删除。
```

 

```
原因2：在applicationContext.xml配置文件中，没有加这个配置文件信息。这是在service层加事物控制的。
<!-- ASPECT CONFIGURATION -->
	<aop:config>
		<aop:pointcut id="monsterManager" expression="execution(* com.ctit.service.*.*(..))" />
		<aop:advisor pointcut-ref="monsterManager" advice-ref="monsterAdvice"/>
	</aop:config>
	
	 
	<tx:advice id="monsterAdvice" transaction-manager="transactionManager">  
		<tx:attributes>
			<tx:method name="insert*" propagation="REQUIRED"/>
			<tx:method name="add*" propagation="REQUIRED"/>
			<tx:method name="update*" propagation="REQUIRED"/>
			<tx:method name="delete*" propagation="REQUIRED"/>
			<tx:method name="register*" propagation="REQUIRED"/>
			<tx:method name="create*" propagation="REQUIRED"/> 
			<tx:method name="*" />
		</tx:attributes>	
	</tx:advice>
```