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

annotation级联删除问题
各位大侠我用注解配置一个多的实体

一个用户 一个代码 为什么我删除多的一方的数据时候跟他与之相关的用户那条数据也被删除了 废话不多说上代码请各位大侠多多指教

用户的
package com.sxt.model;

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.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;



@Entity
@SequenceGenerator(name="sequence",sequenceName="seq_test",allocationSize=1)
@Table(name="msuser")
public class User {

private int userId;

private String userName;

private int sex;

private int age;

private int salary;

private IdCard idCard;

private Set<Address> addSet=new HashSet<Address>();

@OneToOne(cascade=CascadeType.ALL,mappedBy="user")
public IdCard getIdCard() {
return idCard;
}

public void setIdCard(IdCard idCard) {
this.idCard = idCard;
}

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="sequence")
@Column(name="userId",unique=true,nullable=true)
public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}
@Column(name="userName")
public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}
@Column(name="sex")
public int getSex() {
return sex;
}

public void setSex(int sex) {
this.sex = sex;
}
@Column(name="age")
public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
@Column(name="salary")
public int getSalary() {
return salary;
}

public void setSalary(int salary) {
this.salary = salary;
}
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy="user")
public Set<Address> getAddSet() {
return addSet;
}

public void setAddSet(Set<Address> addSet) {
this.addSet = addSet;
}


}
地址的
package com.sxt.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

@Entity
@SequenceGenerator(name="sequence",sequenceName="seq_test",allocationSize=1)
@Table(name="address")
public class Address {
private String addrName;

private int addrId;

private User user;

public Address(){}

public Address(String addrName, int addrId, User user) {
super();
this.addrName = addrName;
this.addrId = addrId;
this.user = user;
}
@Column(name="addr_name")
public String getAddrName() {
return addrName;
}

public void setAddrName(String addrName) {
this.addrName = addrName;
}
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="sequence")