【问题标题】:checkbox click executes many hibernate queries [duplicate]复选框单击执行许多休眠查询[重复]
【发布时间】: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


    【解决方案1】:

    嗯,看起来我“修复”了它。但我比以往任何时候都更不确定我正在做的事情甚至与“一个人应该如何做”密切相关。 如果有人能告诉我我的代码是否完全愚蠢和错误,或者我的方向是否正确,那就太好了。

    我这样更改了我的 profileProducer:

    private Map<String, List<Profile>> cachedProfiles = new HashMap<String, List<Profile>>();
    
    public List<Profile> getProfilesByFormAndName(String formName, String groupName) {
        String key = formName + groupName;
        if (!cachedProfiles.containsKey(key)) {
            List<Profile> profiles = profileRepository.getProfilesByGroup(groupRepository.getGroupByFormAndName(formName, groupName));
            cachedProfiles.put(key, profiles);
        }
        return cachedProfiles.get(key);
    }
    

    显然现在它不会随时查询数据库 - 但我仍然不明白为什么它甚至会执行该方法。

    我是否使用了完全错误的 bean 和/或注释?

    非常欢迎任何责备/意见。

    编辑: 看起来我编写了“解决方法”(缓存,正如我在 balusc 其他答案中提到的那样)。

    编辑2:

    最后,我按照 balus 的建议做了。 我在 @PostConstruct 方法中获取所有数据并将它们放入映射中(以 groupName + formName 作为键),在我的 getter 方法中我只是将它们读出来。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多