2012-07-18 47 views
4

我有一個包含ForeignCollection吸氣劑&引領者收集增加,從ForeignCollection刪除對象

public class TextQuestion{ 

    @ForeignCollectionField 
    private ForeignCollection<TextAnswer> answers; 
    .... 

我有一個封裝了DAO

public class TextQuestionService 
{ 

    private static TextQuestionService instance; 
    private static Dao<TextQuestion, Integer> textQuestionDAO; 
    private static DatabaseHelper dbHelper = new DatabaseHelper(); 


    public void addAnswer(TextQuestion textQuestion, TextAnswer answer) 
    { 
     List<Answer> answers = (List)textQuestion.getAnswers(); 
     if(answers == null) 
     { 
      try 
      { 
       answers = (List)textQuestionDAO.getEmptyForeignCollection("answers"); 
      } catch (SQLException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     answers.add(answer); 
    } 

,維修人員包裝類的實體我的問題是 如何從這個集合通過包裝服務類添加,刪除的項目? 以上是我的心血,但我剛剛完全喪失,鑄造到收藏列表等 問題是,當我要到答案添加到問題ForeignCollection如果答案集合不空就沒有問題,但我想建立問題,在堅持它之前添加答案。從前面的問題我知道我需要調用getEmptyForeignCollection,如果沒有從數據庫中檢索父項。必須有一個簡單的方法來做到這一點?任何工作類似的例子將是很好

回答

6

問題是,當我想要的答案添加到問題ForeignCollection就沒有問題,如果答案收集不空,但我想建立的問題了,

我想你靠近。你應該能夠做到:

ForeignCollection<TextAnswer> answers = textQuestion.getAnswers(); 
if (answers == null) { 
    answers = textQuestionDAO.getEmptyForeignCollection("answers"); 
} 
answers.add(answer); 

answers也可以是一個Collection<TextAnswer>,但它不是一個List