-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Using entities in JSF. Which is the best strategy?
PostPosted: Tue Jun 14, 2011 4:30 am 
Beginner
Beginner

Joined: Wed Jul 30, 2008 8:43 am
Posts: 32
Hi.

This is the global issue, partly linked with the issue here: viewtopic.php?f=1&t=1009617

But would like to hear experts on best practice of editing JPA entites from JSF UI.

So, a couple of words about the problem.

Imagine I have the persisted object MyEntity and I fetch it for editing. In DAO layer I use

Code:
return em.find(MyEntity.class, id);


Which returns MyEntity instance with proxies on "parent" entities - imagine one of them is MyParent. MyParent is fetched as the proxy greeting to @Access(AccessType.PROPERTY):

Code:
@Entity
public class MyParent {

    @Id
    @Access(AccessType.PROPERTY)   
    private Long id;
...
}


and MyEntity has the reference to it:

Code:
    @ManyToOne(fetch = FetchType.LAZY)
    @LazyToOne(LazyToOneOption.PROXY)
    private MyParent myParent;


So far so good. In UI I simply use the fetched object directly without any value objects created and use the parent object in the select list:
Code:
<h:selectOneMenu value="#{myEntity.myParent.id}" id="office">
      <f:selectItems value="#{parents}"/>
   </h:selectOneMenu>


Everything is rendered ok, no LazyInitializationException occurs. But when I save the object I recieve the "LazyInitializationException: could not initialize proxy - no Session" on MyParent proxy setId() method.

I can easily fix the problem if I change the MyParent relation to EAGER

Code:
    @ManyToOne(fetch = FetchType.EAGER)
    private MyParent myParent;

or fetch the object using "left join fetch p.myParent" (actually that's how I do now). In this case the save operation works ok and the relation is changed to the new MyParent object transparently. No additional actions (manual copies, manual references settings) need to be done. Very simple and convenient.

BUT. If the object references 10 other object - the em.find() will result 10 additional joins, which isn't a good db operation, especially when I don't use references objects state at all. All I need - is links to objects, not their state.

This is a global issue, I would like to know, how JSF specialists deal with JPA entities in their applications, which is the best strategy to avoid both extra joins and LazyInitializationException.

Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.