0

I have a class as following need to add people to it, but I just have the id of people, do I need to retrieve all the objects before associating them to 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);
    ...
  }
2
  • 1
    if you have a List<Long> you can't set something expecting List<People> Commented Aug 21, 2013 at 0:22
  • do I need to retrieve all of them ? Commented Aug 21, 2013 at 0:22

1 Answer 1

2

Why don't do this

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);

Also your People class would be better as Person perhaps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.