简体   繁体   English

在不同的会话中取消删除收藏集

[英]Undelete Collections in different sessions

Consider the case given below. 考虑下面给出的情况。

class Person{
@Id
String name;
@OneToMany
List<Address> addresses;
}

Suppose in a session I create a person and one address value and save, and in a different session I do a load and when I persist I will add some addresses and I do not want the first address saved to be deleted then what should I do? 假设在一个会话中创建一个人和一个地址值并保存,在另一个会话中进行加载,当我坚持时,我将添加一些地址,并且我不希望删除保存的第一个地址,那我该怎么办?

Example: 例:

Session session1 =  sf.getCurrentSession();
session1.save(person);//person here has an address diff from below
session1.close();
Session session2 =  sf.getCurrentSession();
Person p1=session2.load(person,"Name");
List persA=new Arraylist<Address>();persA.add(new Address(...));
p1.setAddress(persA);
session.merge(p1);
session2.close();//this code is not exact I want to just project concept from here

So I want both the addresses to remain in the db and when I eagerly load thru spring data i should get both the addresses! 因此,我希望两个地址都保留在数据库中,当我急切地通过spring数据加载时,我应该同时获得两个地址! how can i get this behavior? 我如何获得这种行为?

Thanks all! 谢谢大家!

Well you are overwriting the address list by creating the new one. 那么,您正在通过创建新地址列表来覆盖地址列表。 once you load the Person do the following: 加载Person请执行以下操作:

List persA = p1.getAdress(); //get the existing address associated to the person.
persA.add(new Address()); //add new address to the existing list.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM