org.hibernate.HibernateException: A collection with cascade="alldeleteorphan" was no longer referenced by the owning entity instance:

如果你是以上的错误请以下

hibernate  级联更新并删除,方法如:实体users与T,OneToMany关系。更新users表时级联删除、更新、添加T

  1 import java.util.HashSet;
  2 import java.util.Set;
  3 import javax.persistence.CascadeType;
  4 import javax.persistence.Column;
  5 import javax.persistence.Entity;
  6 import javax.persistence.FetchType;
  7 import javax.persistence.Id;
  8 import javax.persistence.OneToMany;
  9 import javax.persistence.Table;
 10 
 11 import org.hibernate.annotations.Cascade;
 12 
 13 /**
 14  * Users entity.
 15  * 
 16  * @author MyEclipse Persistence Tools
 17  */
 18 @Entity
 19 @Table(name = "users", catalog = "test")
 20 public class Users implements java.io.Serializable {
 21 
 22     // Fields
 23 
 24     private String uuid;
 25     private String userName;
 26     private String passwd;
 27     private Integer gender;
 28     private Set<T> ts = new HashSet<T>(0);
 29 
 30     // Constructors
 31 
 32     /** default constructor */
 33     public Users() {
 34     }
 35 
 36     /** minimal constructor */
 37     public Users(String uuid, String userName, String passwd) {
 38         this.uuid = uuid;
 39         this.userName = userName;
 40         this.passwd = passwd;
 41     }
 42 
 43     /** full constructor */
 44     public Users(String uuid, String userName, String passwd, Integer gender,
 45             Set<T> ts) {
 46         this.uuid = uuid;
 47         this.userName = userName;
 48         this.passwd = passwd;
 49         this.gender = gender;
 50         this.ts = ts;
 51     }
 52 
 53     // Property accessors
 54     @Id
 55     @Column(name = "UUID", unique = true, nullable = false, length = 32)
 56     public String getUuid() {
 57         return this.uuid;
 58     }
 59 
 60     public void setUuid(String uuid) {
 61         this.uuid = uuid;
 62     }
 63 
 64     @Column(name = "userName", nullable = false, length = 20)
 65     public String getUserName() {
 66         return this.userName;
 67     }
 68 
 69     public void setUserName(String userName) {
 70         this.userName = userName;
 71     }
 72 
 73     @Column(name = "passwd", nullable = false, length = 32)
 74     public String getPasswd() {
 75         return this.passwd;
 76     }
 77 
 78     public void setPasswd(String passwd) {
 79         this.passwd = passwd;
 80     }
 81 
 82     @Column(name = "gender")
 83     public Integer getGender() {
 84         return this.gender;
 85     }
 86 
 87     public void setGender(Integer gender) {
 88         this.gender = gender;
 89     }
 90 
 91     @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "users")
 92     @Cascade(value = { org.hibernate.annotations.CascadeType.ALL,
 93             org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
 94     public Set<T> getTs() {
 95         return this.ts;
 96     }
 97 
 98     private void setTs(Set<T> ts) {
 99         this.ts.clear();
100         this.ts = ts;
101     }
102     
103     public void addTs(T t){
104         getTs().add(t);
105         t.setUsers(this);
106 
107     }
108 
109 }

实体T

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 * T entity.
 * 
 * @author MyEclipse Persistence Tools
 */
@Entity
@Table(name = "t", catalog = "test")
public class T implements java.io.Serializable {

	// Fields

	private String id;
	private Users users;

	// Constructors

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

	/** full constructor */
	public T(String id, Users users) {
		this.id = id;
		this.users = users;
	}

	// Property accessors
	@Id
	@Column(name = "id", unique = true, nullable = false, length = 32)
	public String getId() {
		return this.id;
	}

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

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "t1", nullable = false)
	public Users getUsers() {
		return this.users;
	}

	public void setUsers(Users users) {
		this.users = users;
	}

}

需求:

1、删除

1   Users user = (Users) session.get(Users.class,"1");
2   user.getTs().clear();
3   user.setPasswd("144");
4   session.update(user);

2、添加

1   Users user = (Users) session.get(Users.class,"1");
2   user.getTs().clear();
3   T t = new T("2",user);
4   user.addTs(t);
5   user.setPasswd("144");
6   session.update(user);

3、更新

原文地址:https://www.cnblogs.com/fengqingtao/p/2761109.html