我使用JavaEE6和JSF制作简单的CRUD应用程序。
在我的许多JSF页面中,我都有一个selectonemenu供用户选择一个现有的项目。例如,如果用户要添加一个“检查”,他/她可以从组合框中选择一个“部门”,因为他们有一对多的关系。
问题是,每当添加新的部门时,组合框值在会话超时之前不会更新。我需要使用sessionscoped支持bean,因为我需要在多个请求之间保持这些值。
下面是我用来填充SelectioneMenu的函数(与上面描述的示例不完全相同,但非常相似):
public SelectItem[] getExamsByDepartment(){
if(departmentMaster!=null){
Collection<ExamMaster> examMasterCollection = departmentMaster.getExamMasterCollection();
//Problem: newly added exams aren't shown until session is re-created
if (examMasterCollection != null && examMasterCollection.size() > 0) {
SelectItem[] selectItem = new SelectItem[examMasterCollection.size()];
Iterator<ExamMaster> i = examMasterCollection.iterator();
int count = 0;
while (i.hasNext()) {
ExamMaster tmpExamMaster = i.next();
selectItem[count++] = new SelectItem(tmpExamMaster, tmpExamMaster.getExamName());
}
return selectItem;
}
}
SelectItem[] selectItem = new SelectItem[1];
selectItem[0] = new SelectItem("", "[No exams found]");
return selectItem;
}
是否有任何解决方法可以销毁并重新创建会话?或者其他我能解决这个问题的方法?
编辑:我想问题归根结底就是为什么在插入记录后集合不更新,即使一对多关系是在持久性类中定义的。
编辑2:我现在使用的不是集合,而是命名查询,它按预期提取新记录。我假设集合应该自动更新。也许那不是真的?