日期:2014-05-20  浏览次数:20776 次

jpa的级联删除 在SSH2框架内执行失败,求指导啊~~
城市类
Java code

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;
    }

}




车库类
Java code

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);
    }
}




汽车类 
Java code

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;
    }
}