0

我有一个类,如下需要添加人员,但我只有人员的 id,在将它们关联到 myList 之前是否需要检索所有对象?

class People{

 private long id;

 private String name;
 ....
}

class myList{

 private long id;

 @OneToMany
 private List<People> people;
 ...
}

class myModel{
  ....
  {
    List long selectedPeople  <<< has ids such as 8 9 7 6 45 6
    myList mylist = new MyList();
    mylist.setPeople(selectedPeople);
    session.save(mylist);
    ...
  }
4

1 回答 1

2

为什么不这样做

List<People> toSave = new ArrayList<>();

for(Long id : selectedPeople){
   People people = new People();
   people.setId(id);
   toSave.add(people);
}
myList.setPeople(toSave);
session.save(myList);

你的People课也可能会更好Person

于 2013-08-21T00:24:25.157 回答