2013-08-21 61 views
0

我有一個類如下需要添加人員,但我只有人的id,我需要檢索所有對象之前將它們關聯到myList?如何使用ID插入記錄列表?

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); 
    ... 
    } 
+1

如果你有一個'List '你不能設置什麼期望'List ' – nachokk

+0

我需要檢索它們嗎? –

回答

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也許更好。

相關問題