2013-07-31 75 views
0

我有一個Web應用程序(使用Vaadin,Hibernate和Postgres數據庫),用戶可以在其中將數據添加到某個表。當用戶將一個項目添加到這個表格中時,他們被要求輸入名稱,日期以及從表格中選擇的所有相關產品。當他們點擊保存時,我可以將名稱和日期保存到數據庫中,但無法從表格中抓取項目並將其放入要添加的集合中。 bean類的表看起來像這樣:保存在Web應用程序中

/** Created by Hibernate **/ 
public class BeanClass implements Serializable { 
    private String name; 
    private Date date; 
    private Set relatedProducts = new HashSet(0); 
    . 
    . 
    . 
    } 

我使用,以節省用戶想要添加項目的代碼:

BeanClass toAdd = new BeanClass(); 
Set temp = null 

/** There is a table where the user can select all the products they want to add 
* When they select an item it goes to another table, so what I am doing here 
* is adding looping through latter table and adding all the items they selected 
* to a set (supposedly i think this should work 
**/ 
for (Object item : table) { 
    temp.add(table.getContainerProperty("id", item)); 
} 

toAdd.setName(nameField.getValue()); //I have input fields for the name and date 
toAdd.setDate(dateField.getValue()); 
toAdd.setRelatedProducts(temp); 

hbsession.save(toAdd); 

當用戶點擊保存名稱和日期添加到表中,但相關產品不會添加到關係表(在數據庫中)。我在冬眠期間考慮了級聯和反轉,並且認爲我將不得不使用這些,但不太明白如何。

回答

1

什麼數據類型是您relatedProducts設置?它是另一個Bean,那麼你必須定義一個OneToMany註解。

如果在寫入數據庫之前填充了Set,那麼您是否進行了調試?

你真的不需要迭代整個表來獲得選定的值。你可以得到()()通過table.getValue選擇的項目

的getValue

獲取所選項目的ID或者多選模式一組選定的ID。

如果是在表中相同的數據類型,因爲它是在Bean中,然後

toAdd.setRelatedProducts(table.getValue()); 

應該夠了。

+0

relatedProucts應該是一個整數((1,2),(1,4),(1,6))的元組,因此當用戶單擊保存時,它將添加3與1的關係。在vaadin表中,類型組件。 – abden003