interboy
interboy

Reputation: 886

Dynamic content from a backing bean not being updated immediately in the view

I am working on a Java EE application where the user can register some units. I want that each unit to be displayed on a separated tab when the user is logged in.

<p:tabView id="unitTabs" cache="false" var="unit" value="#{getUnitsOfLoggedInUser(loginController.checkedUser)}"> 
        <p:tab id="unitTab" title="#{unit.unitName}">  

            //content here

        </p:tab>  
</p:tabView>

In the backing bean I have this method

public List<Unit> getUnitsOfLoggedInUser(User user) {
    unitsOfLoggedInUser = (List<Unit>) user.getUnitCollection();
    return unitsOfLoggedInUser;
}

However, if the user creates a new unit, the new tab will not be rendered immediately. Even if I refresh the page, it still is not displayed. However, if I restart the server and run again, the new tab will be there. It seems that it has some caching property and does not load on each call.

Also, when I debug the code the method getUnitsOfLoggedInUser(user) is called more than one time before the page is rendered.

I have also tried with a JSF data table but it behaves the same, so it is not a problem of the Primefaces library

Does anyone have any suggestion how can I solve this? Thanks

Upvotes: 1

Views: 475

Answers (1)

Matt Handy
Matt Handy

Reputation: 30025

The answer depends on the way you save new units. One option is to refresh the user entity after saving a new unit. In any case you could do

em.refresh(user);

after saving, where em is your instance of the EntityManager.

Update:

You could also add the new unit to the collection of the user and then merge/edit the user instead of directly creating the new unit. Then you wouldn't need to refresh the user because you manually added the unit to the collection.

Upvotes: 3

Related Questions