jpa的级联删除 在SSH2框架内执行失败,求指导啊~~
城市类
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;//城市类@Entitypublic 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;//车库类@Entitypublic 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;//汽车类 @Entitypublic 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; }}
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;@Repositorypublic 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); }}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@Transactionalpublic 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); }}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); }}