简体   繁体   中英

hibernate lazy loading not updated

Hi im working with jboss on a webapp. I got one page that is similar to forum page where you can post messages and reply to already posted ones. In my jsf i have three div tags one for adding new message one for listing all messages and one for viewing selected message. All the tags are embedded in and there is render attribute on every tag something like this:

<h:pannelGroup rendered="#{myController.shouldRender('add')}">

<!-- Here is my html for adding new message -->

</h:pannelGroup>


<h:pannelGroup rendered="#{myController.shouldRender('list')}">

<!-- Here is my html for listing messages -->

</h:pannelGroup>

<h:pannelGroup rendered="#{myController.shouldRender('view')}">

<!--    Here is my html for viewing message and its replys.. 
    also there is hidden div with html for popup to post reply -->


<div id="reply">
<!--    This is hidden html that is shown when clicked reply 
        link in the message div below. 
        When shown users can add reply to the message -->
</div>

<div id="message">
<!-- Here is show the message itself -->
</div>

<div id="replyList">
<!-- Here is list replys for the message currently beeing viewed 

    For listing the replys i used ui:repeate and c:forEach from the jstl core
    both resulting with same outcome.

    In my message object i have list of replys which i load lazily...
-->
</div>

</h:pannelGroup>

My backing bean, stripped of all the annotations and rest of the code...

MyController{

String page;

public boolean shouldRender(String view){
    return page.equals(view);
}
}

The page property i set with actionListener from list of menu items, before i redirect the user to the message.xhtml page i set myController's property page to the div name i want to view, so for eg if i click the add link i set the page = "add" and redirect to the message.xhtml. There the controller picks up the page set from outside and renders the add div.

Because i couldn't manage to get extended persistence context working, i set filter on /* to open user transaction and then merge the entity manager with that one, after the chain.DoFilter i commit my transaction. This i needed to enable lazy loading by hand..

The problem is that when i add reply message, the list with replies does not get refreshed immediately, i need to go back to the message list and then again open the same message in the view div to get the list of replies refreshed.. or... in the method for adding reply i tried to manually load my reply inside the list of replies owned by the message object (which are lazy loaded and mapped @OneToMany) and this way it works but i don't like that solution.

Can someone tell me weather hibernate is reloading the lazy loaded list because i manage the transaction and i assume that once it loaded my list, it doesn't refresh it on it's own.

When the list is modified in the same session, it is updated.

When the list is modified in a different session, it does not get updated.

As the session object is not multi-thread safe, in your environment probably every user has his own session instance, so you'll be in the second case. To force a refresh, you can evict() the parent instance and load() it again, or you clear() the session or you create and use a new one.

Pay attention with lazy loading. As the child elements are loaded on first usage (and not together with the parent instance), they can already reflect to a modification which was not yet made when the parent object was loaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM