删除实体外键关系弹簧

时间:2017-02-15 06:24:18

标签: java spring-data

我有两张桌子彼此关系。 当我删除父表中的一行数据时。 如果表中使用的数据流,则出现错误消息。 如果没有,删除数据enter image description here
SQL Server外键更新和删除规则

无动作:不允许。将生成错误消息。 (我想使用此例外)

删除Ma_DM_NGAN_HANG中的行抛出异常

@Entity

@Table(name =“Ma_DM_NGAN_HANG”,schema =“dbo”,uniqueConstraints = @UniqueConstraint(columnNames =“MANH”)) 公共类DmNganHang实现了java.io.Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
// Fields

private Long id;
private String manh;
private String tennh;
private String tentat;
private String diachi;
private String dienthoai;
private String fax;
private String email;
private String taikhoan;
private String masothue;
private Integer idDonvi;
private String website;
private String filter;
private List<DmNhanVien> dmNhanViens = new ArrayList<DmNhanVien>();
private List<DmDoiTacTknganhang> dmDoiTacTknganhangs = new ArrayList<DmDoiTacTknganhang>();

// Constructors

/** default constructor */
public DmNganHang() {
}

/** minimal constructor */
public DmNganHang(Long id, String manh, String tennh, String taikhoan) {
    this.id = id;
    this.manh = manh;
    this.tennh = tennh;
    this.taikhoan = taikhoan;
}

/** full constructor */
public DmNganHang(Long id, String manh, String tennh, String tentat,
        String diachi, String dienthoai, String fax, String email,
        String taikhoan, String masothue, Integer idDonvi, String website,
        String filter, List<DmNhanVien> dmNhanViens,
        List<DmDoiTacTknganhang> dmDoiTacTknganhangs) {
    this.id = id;
    this.manh = manh;
    this.tennh = tennh;
    this.tentat = tentat;
    this.diachi = diachi;
    this.dienthoai = dienthoai;
    this.fax = fax;
    this.email = email;
    this.taikhoan = taikhoan;
    this.masothue = masothue;
    this.idDonvi = idDonvi;
    this.website = website;
    this.filter = filter;
    this.dmNhanViens = dmNhanViens;
    this.dmDoiTacTknganhangs = dmDoiTacTknganhangs;
}

// Property accessors
@Id
@Column(name = "ID", unique = true, nullable = false, precision = 18, scale = 0)
public Long getId() {
    return this.id;
}

public void setId(Long id) {
    this.id = id;
}

// Code ......

@JsonInclude(JsonInclude.Include.NON_EMPTY)
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "dmNganHang")
public List<DmNhanVien> getDmNhanViens() {
    return this.dmNhanViens;
}

public void setDmNhanViens(List<DmNhanVien> dmNhanViens) {
    this.dmNhanViens = dmNhanViens;
}

@JsonInclude(JsonInclude.Include.NON_EMPTY)
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "dmNganHang")
public List<DmDoiTacTknganhang> getDmDoiTacTknganhangs() {
    return this.dmDoiTacTknganhangs;
}

public void setDmDoiTacTknganhangs(
        List<DmDoiTacTknganhang> dmDoiTacTknganhangs) {
    this.dmDoiTacTknganhangs = dmDoiTacTknganhangs;
}

}

@Entity @Table(name =“Ma_DM_NHAN_VIEN”,schema =“dbo”,uniqueConstraints = @UniqueConstraint(columnNames = {         “MANV”,“IdDonvi”})) 公共类DmNhanVien实现了java.io.Serializable {

// Fields

/**
 * 
 */
private static final long serialVersionUID = 1L;
private Long id;
private DmNganHang dmNganHang;
private DmPhongBan dmPhongBan;
private String manv;
private String tennv;
private String diachi;
private String hkthuongtru;
private String dienthoai;
private String masothue;
private String socmt;
private Date ngaycap;
private String noicap;
private Boolean canhancutru;
private String nhom;
private String taikhoannh;
private String tinhtranghonnhan;
private Boolean gioitinh;
private Date ngaysinh;
private Integer idDonvi;
private String filter;
private List<TnChungtuthunhap> tnChungtuthunhaps = new ArrayList<TnChungtuthunhap>();
private List<TnNguoiphuthuoc> tnNguoiphuthuocs = new ArrayList<TnNguoiphuthuoc>();
private List<DmHopDong> dmHopDongs = new ArrayList<DmHopDong>();

// Constructors

/** default constructor */
public DmNhanVien() {
}

/** minimal constructor */
public DmNhanVien(Long id, DmNganHang dmNganHang, DmPhongBan dmPhongBan,
        String manv, String tennv, String diachi, String taikhoannh) {
    this.id = id;
    this.dmNganHang = dmNganHang;
    this.dmPhongBan = dmPhongBan;
    this.manv = manv;
    this.tennv = tennv;
    this.diachi = diachi;
    this.taikhoannh = taikhoannh;
}

/** full constructor */
public DmNhanVien(Long id, DmNganHang dmNganHang, DmPhongBan dmPhongBan,
        String manv, String tennv, String diachi, String hkthuongtru,
        String dienthoai, String masothue, String socmt, Date ngaycap,
        String noicap, Boolean canhancutru, String nhom, String taikhoannh,
        String tinhtranghonnhan, Boolean gioitinh, Date ngaysinh,
        Integer idDonvi, String filter,
        List<TnChungtuthunhap> tnChungtuthunhaps,
        List<TnNguoiphuthuoc> tnNguoiphuthuocs, List<DmHopDong> dmHopDongs) {
    this.id = id;
    this.dmNganHang = dmNganHang;
    this.dmPhongBan = dmPhongBan;
    this.manv = manv;
    this.tennv = tennv;
    this.diachi = diachi;
    this.hkthuongtru = hkthuongtru;
    this.dienthoai = dienthoai;
    this.masothue = masothue;
    this.socmt = socmt;
    this.ngaycap = ngaycap;
    this.noicap = noicap;
    this.canhancutru = canhancutru;
    this.nhom = nhom;
    this.taikhoannh = taikhoannh;
    this.tinhtranghonnhan = tinhtranghonnhan;
    this.gioitinh = gioitinh;
    this.ngaysinh = ngaysinh;
    this.idDonvi = idDonvi;
    this.filter = filter;
    this.tnChungtuthunhaps = tnChungtuthunhaps;
    this.tnNguoiphuthuocs = tnNguoiphuthuocs;
    this.dmHopDongs = dmHopDongs;
}

// Property accessors
@Id
@Column(name = "ID", unique = true, nullable = false, precision = 18, scale = 0)
public Long getId() {
    return this.id;
}

public void setId(Long id) {
    this.id = id;
}

@JsonIgnore
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "ID_NGANHANG", nullable = false)
public DmNganHang getDmNganHang() {
    return this.dmNganHang;
}

public void setDmNganHang(DmNganHang dmNganHang) {
    this.dmNganHang = dmNganHang;
}

@JsonIgnore
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "ID_PHONGBAN", nullable = false)
public DmPhongBan getDmPhongBan() {
    return this.dmPhongBan;
}

// Code .....

}

方法删除

public <T, E extends Serializable> void delete(T entity) {
    entity = entityManager.merge(entity);
    entityManager.remove(entity);
}
public <T, E extends Serializable> void deletefindOne(T entity) {
    E id = (E) CommonUtil.invoke(entity, "getId");
    JpaRepository repository = new SimpleJpaRepository(entity.getClass(),
            entityManager);
    repository.delete(repository.findOne(id));
}

由于

0 个答案:

没有答案