开发者社区> 问答> 正文

请问jpa删除实体有级联关系删不掉怎么办?

就是主要是想删除一个班里的某一个学生但不想影响老师那张表中的数据,delete 不掉,报异常"Error while committing the transaction"

展开
收起
蛮大人123 2016-06-13 16:24:31 4022 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
    /**  
     * many to one 一对多关联 
     */ 
    
    import java.util.HashSet;   
    import java.util.Set;   
    import javax.persistence.CascadeType;   
    import javax.persistence.Column;   
    import javax.persistence.Entity;   
    import javax.persistence.FetchType;   
    import javax.persistence.GeneratedValue;   
    import javax.persistence.Id;   
    import javax.persistence.OneToMany;   
    @Entity  
    public class Garage {   
        private Integer gid;   
        private String garagenum;   
        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;   
        }   
        public void addGarageAuto(Auto auto) {   
            auto.setGarage(this);   
            this.autos.add(auto);   
        }   
    }

    Auto.java

    /**  
     * one to many 多对一关联  
     */ 
    
    import javax.persistence.CascadeType;   
    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;   
        }   
    }  
    2019-07-17 19:36:21
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载