<h: selectOneMenu> and lazy boot from sleep mode

In my JSF application, I have a pretty simple piece of code. I want the user to select one value from the collection in <h:selectOneMenu>:

<h:selectOneMenu value="#{bean.value}">
  <f:selectItems value="#{dao.valuesFromDb}" />
</h:selectOneMenu>

#{bean.value}has a type Region, but #{dao.valuesFromDb}returns a list Regions. The problem is that Regionthe bean lazily loads from sleep mode, which wraps it in some shell, the class looks like my.package.Region_$$_javassist_15@25183.

If it #{bean.value}has a value, it must be preselected on the page if it is in the list of values ​​( #{dao.valuesFromDb}). The problem is that it is checked by a method equalsthat returns false, because the types are different.

How to solve this problem? Is it possible to make JSF not use equals, but somehow handle this comparison? Overriding equals, therefore, it ignores the IMHO type, a really bad idea, as it can break symmetry equals.

+3
source share
3 answers

In this case, I would have avoided the problem altogether, looking impatiently Regionat bean.

Flushing with equals () will lead to much less clear code than just getting the information that JSF wants to execute. And, as you point out, this can have unintended consequences.

0
source

Override equals. equals, . Hibernate, , javassist.

Region equals :

@Override
public boolean equals(Object obj) {
    if (this == obj) {return true;}
    if (obj == null) {return false;}
    if (!(obj instanceof Region)) {return false;}

    Region region = (Region) obj;

    if ((name == null) && (region.getName() != null)) {return false;}  // Do NOT use 'region.name' here
    else if (!name.equals(region.getName())) {return false;}           // Do NOT use 'region.name' here
    return true;
}
0

Instead of changing the selection type in EAGER, you can preload the bean by calling the getter Regionin action (of the controller), which will go to your view. Ie: just call the method bean.getRegion().

This way you will not get a LazyInitializationException (if that is what you get).

0
source

All Articles