城市类package wxm.domain;import java.util.HashSet;
import java.util.Set;import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;//城市类
@Entity
public class City { @Id
@GeneratedValue
private Integer cId; @Column(length = 20)
private String cName; // CascadeType属性有四个值,其中REMOVE属性是实现级联删除,要实现级联删除
// 在父栏必需添加CascadeType.REMOVE标注,这是级联删除的关键
@OneToMany(cascade = { CascadeType.REMOVE }, mappedBy = "city")
private Set<Garage> garages = new HashSet<Garage>(0); public Integer getcId() {
return cId;
} public void setcId(Integer cId) {
this.cId = cId;
} public String getcName() {
return cName;
} public void setcName(String cName) {
this.cName = cName;
} public Set<Garage> getGarages() {
return garages;
} public void setGarages(Set<Garage> garages) {
this.garages = garages;
}}车库类package wxm.domain;import java.util.HashSet;
import java.util.Set;import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;//车库类
@Entity
public class Garage {
private Integer gid;
private String garagenum;
private City city;
private Set<Auto> autos = new HashSet<Auto>(); @Id
@GeneratedValue
public Integer getGid() {
return gid;
} public void setGid(Integer gid) {
this.gid = gid;
} @Column(length = 20)
public String getGaragenum() {
return garagenum;
} public void setGaragenum(String garagenum) {
this.garagenum = garagenum;
} // CascadeType属性有四个值,其中REMOVE属性是实现级联删除,要实现级联删除
// 在父栏必需添加CascadeType.REMOVE标注,这是级联删除的关键
@OneToMany(cascade = { CascadeType.REMOVE }, mappedBy = "garage")
public Set<Auto> getAutos() {
return autos;
} public void setAutos(Set<Auto> autos) {
this.autos = autos;
} @ManyToOne()
@JoinColumn(name = "cityId")
public City getCity() {
return city;
} public void setCity(City city) {
this.city = city;
} public void addGarageAuto(Auto auto) {
auto.setGarage(this);
this.autos.add(auto);
}
}汽车类 package wxm.domain;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;//汽车类 
@Entity
public class Auto {
private Integer autoId;
private String autotype;
private String autonum;
private Garage garage; @Id
@GeneratedValue
public Integer getAutoId() {
return autoId;
} public void setAutoId(Integer autoId) {
this.autoId = autoId;
} public String getAutotype() {
return autotype;
} public void setAutotype(String autotype) {
this.autotype = autotype;
} public String getAutonum() {
return autonum;
} public void setAutonum(String autonum) {
this.autonum = autonum;
} @ManyToOne()
@JoinColumn(name = "garageid")
public Garage getGarage() {
return garage;
} public void setGarage(Garage garage) {
this.garage = garage;
}
}
dao层package wxm.dao.impl;import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;import org.springframework.stereotype.Repository;import wxm.dao.CityDao;
import wxm.domain.City;@Repository
public class CityDaoBean implements CityDao { @PersistenceContext
private EntityManager em; @Override
public void addCity(City city) {
em.persist(city);
} @Override
public void delCity(City city) {
em.remove(city);
} @Override
public City findCityById(Integer cityId) {
return em.find(City.class, cityId);
}}
其他两个类,写法一样这里就省略了 service层package wxm.service.impl;import javax.annotation.Resource;import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import wxm.dao.CityDao;
import wxm.domain.City;
import wxm.service.CityService;@Service
@Transactional
public class CityServiceBean implements CityService { @Resource
private CityDao cityDao; @Override
public void addCity(City city) {
cityDao.addCity(city);
} @Override
public void delCity(City city) {
cityDao.delCity(city);
} @Override
public City findCityById(Integer cityId) {
return cityDao.findCityById(cityId);
}}junit测试方法package junit;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import wxm.domain.City;
import wxm.service.AutoService;
import wxm.service.CityService;
import wxm.service.GarageService;public class CT { private static AutoService autoService;
private static CityService cityService;
private static GarageService garageService; @BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
autoService = (AutoService) ctx.getBean("autoServiceBean");
cityService = (CityService) ctx.getBean("cityServiceBean");
garageService = (GarageService) ctx.getBean("garageServiceBean");
} catch (Exception e) {
e.printStackTrace();
}
} @Test
public void test() {
City city = new City();
city.setcName("北京");
cityService.addCity(city);
}

@Test
public void del()
{
City city = cityService.findCityById(2);
cityService.delCity(city);
}}除了删除所有都可以正常执行。 
如果不使用框架 直接用entityManager来手动控制事务也能执行成功。
报错语句:java.lang.IllegalArgumentException: Removing a detached instance wxm.domain.City#2
改了很多地方都不对,救命啊高手…… 要怎么才能让它在框架内执行成功,并能级联删除……