facebook

Lazyload roadblock

  1. MyEclipse IDE
  2.  > 
  3. Spring Development
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #319716 Reply

    Greg Soulsby
    Member

    I have been staring at this for so Iong I am beginning to wonder if the things people say about me are true.

    Within Spring Webflow, have a form that edits a Child record, within a Parent Child relationship. The form needs to present details of the Parent entity as well as the Child. As I understand it the Parent is scaffolded as an object attribute of the child.

    So to give the View access to both the Parent and the child I have in the transition to the view

    <transition on=”editRelationship” to=”zzz_editRelationships”>
    <evaluate expression=”childDAO.findChildsByPrimaryKey(requestParameters.chilIdIdKey)” result=”flowScope.child” result-type=””/>
    <evaluate expression=”childDAO.findChildsByPrimaryKey(requestParameters.chilIdIdKey).parent” result=”flowScope.parent” result-type=””/>
    </transition>

    But when I access ${parent.attribute} within the form it gives

    org.hibernate.LazyInitializationException: could not initialize proxy – no Session

    Can you advise?

    Thanks

    #319930 Reply

    jkennedy
    Member

    Did you get this resolved?

    Let me know if you didn’t find the solution.

    Thanks,
    Jack

    #319934 Reply

    Greg Soulsby
    Member

    No, have not made progress.

    #319950 Reply

    jayperkins
    Member

    Hi,

    The problem is caused by how the spring webflow alwaysRedirectOnPause (AKA Post-Redirect-Get) functionality works. See the link below:
    http://www.ervacon.com/products/swf/tips/tip4.html

    Your second evaluate expression will return a proxy (not an actual instance) for the parent property because you are accessing a relationship that is marked with a fetch type LAZY. No worries, yet, but then webflow sends a redirect to the client – it hasn’t actually rendered the page yet, but sending the redirect causes the parent property to become disconnected because the session is cloesed when the redirect is sent back to the client. Now when the get request is handled the view is rendered but it can not resolve the proxy because the session is closed.

    Follow these steps to get around this problem:
    1) Remove the second evaluate element from your editRelationship transition.
    2) In your zzz_editRelationships view add the following elements:
    <on-render>
    <evaluate expression=”childDAO.findChildsByPrimaryKey(child.id).parent” result=”flowScope.parent” result-type=””/>
    </on-render>

    Note that in the on-render which is actually the second request, you do not have access to request parameters. But you have already loaded the child record into the child variable, we can get the key values from the child variable. You may have to change it to access your actual key field(s).

    Let me know if you have any questions.

    Jay

    #319958 Reply

    Greg Soulsby
    Member

    Thanks for the thorough reply.

    Doesnt this mean that I need to change every child object definition? To include the foreign key value?

    My understanding is this means enhancing the childServiceImpl.java to include the extra field. So wherever there is

    @OneToMany(fetch=FetchType.LAZY) …

    I should add a new field for the foreign key

    @Column(name=……

    Then I would need to add setter and getter for the new field, which not only sets / gets the new integer field but the Parent Object field as well

    Then I can update all the JPS pages that show the Child to include the foreign key.

    Right? Anything else? Wouldnt there be some update SQL somewhere?

    If you could kindly confirm I have this right then I will update for this specific case. Then the only option would be up update the scaffolding process to do all this for all child records.

    Greg

    #319973 Reply

    jayperkins
    Member

    The code generated by the scaffolding engine for webflow does not need to have these changes applied manually. I thought that you were doing some manual enhancements and that was the reason you had to apply the workaround.

    Can you describe what you are trying to accomplish so that I may provide better guidance? It would be helpful to know what your schema looks like and what the scaffolding engine does not generate that might require manual changes.

    Thanks,

    Jay

    #320000 Reply

    Greg Soulsby
    Member

    Jay,

    I have a parent child relationship and want to edit which parent the child has from the child form. The scoffolded child form does not create field/s for the parent at all. In trying to add a field I want to use the parent object’s attribute on the child, say to output the id of the parent. But I am not able to access the parent objects attribute on the child record, getting the lazy load problem.

    Not sure what I can provide by way of a schema that would be helpful. The parent has a unique Id that is an int, the child has an attribute that is called parent_id. The tables are of type InnoDB within MySQL which means I have created a one to many relationship between the parent and child, the database enforces referential integrity.

    I guess you get this already.

    But you have already loaded the child record into the child variable, we can get the key values from the child variable. You may have to change it to access your actual key field(s).

    What I thought you were saying was to add an extra field to the child domain, being the foreign key, int value of the parent. That would be eagerly fetched, thus enabling a find on the parent from the Flow. So I have misunderstood what “it” is?

    Greg

    #320013 Reply

    jayperkins
    Member

    My apologies, I see how my response could have been confusing.

    What I was trying to say in the quote in your previous post is that I don’t know the name of the key field for your child entity. So in the call to findChildsByPrimaryKey, if the name of the key field was not id, but actuallly childId you would have to change the expression to childDAO.findChildsByPrimaryKey(child.childId).parent.

    I am sure you could have figured that out yourself.

    So I think you are good to go without adding the extra field, etc. Let me know if that is not the case.

    Jay

    #320349 Reply

    Greg Soulsby
    Member

    This has been bugging me since first using MyEclipse 12+months ago, and now it works.

    I am so happy I could cry.

    Many thanks

Viewing 9 posts - 1 through 9 (of 9 total)
Reply To: Lazyload roadblock

You must be logged in to post in the forum log in