【发布时间】:2013-12-23 11:55:22
【问题描述】:
我正在使用 Jboss。
我有一堆通过生产者(@Named, @SessionScoped)生成的复选框,数据来自 mysql 数据库(使用休眠)。当我单击一个复选框时,我会根据单击的复选框打印出(p:growl)一条消息(带有p:ajax)。这一切都有效。但是每次我单击一个复选框时,我都可以看到休眠执行许多不需要的查询。实际上,单击复选框时不应该执行 SINGLE 查询,因为我调用的方法仅将 profile 作为参数并从它的字段发布消息。
以下是相关代码:
jsf部分:
<p:growl id="checkMessages" />
<p:dataTable var="_profile" value="#{profileProducer.getProfilesByFormAndName('test','test')}" >
<p:column>
<p:selectBooleanCheckbox value="#{orderController.checkedProfiles[_profile]}">
<p:ajax update="@([id$=checkMessages])" listener="#{profileProducer.profileCheck(_profile)}" />
</p:selectBooleanCheckbox>
</p:column>
</p:dataTable>
配置文件控制器:
@Named
@SessionScoped
public class ProfileController implements Serializable {
private List<Profile> temporaryCheckedProfileList = new ArrayList<Profile>();
public void profileCheck(Profile profile) {
System.out.println(profile);
String message = profile.getMessage();
if (message == null || message.equals(""))
return;
if (!temporaryCheckedProfileList.contains(profile)) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(message));
temporaryCheckedProfileList.add(profile);
} else {
temporaryCheckedProfileList.remove(profile);
}
}
}
profileProducer:
@RequestScoped
@Named
public class ProfileProducer {
@Inject
private ProfileRepository profileRepository;
@Inject
private GroupRepository groupRepository;
public List<Profile> getProfilesByFormAndName(@New String formName,@New String groupName) {
return profileRepository.getProfilesByGroup(groupRepository.getGroupByFormAndName(formName, groupName));
}
}
这些是我第一次打开网站时执行的查询(这是正确和预期的行为):
Hibernate: select * from group group0_ inner join form form1_ on group0_.form_id=form1_.id where group0_.name=? and form1_.name=? limit ?
Hibernate: select * from profile profile0_ inner join group_profile groupprofi1_ on profile0_.id=groupprofi1_.profile_id inner join group group2_ on groupprofi1_.group_id=group2_.id where group2_.id=1 order by groupprofi1_.sort_nr asc
但是当我点击一个复选框时,我看到上面的两个查询都执行了多次 - 对于某些复选框,它执行 15 次,其他 25 次等等......
我做错了什么?
【问题讨论】:
标签: ajax hibernate jsf-2 primefaces jboss7.x