Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FetchType.LAZY and FetchMode.JOIN

 
Yogesh Lonkar
Ranch Hand
Posts: 94
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have Entity Class something like this



i use to get lazy initialization exception because of parentId for resolving that issue i used @Fetch(FetchMode.JOIN) on all the elements which are in relation with this class.
Now my exception is gone But i am wondering Is this wrong solution like Making "FetchType.EAGER" instead of LAZY and making < bean ...... lazy-init="false">........</bean> which throws away LAZY loading of objects.
Does using
@Fetch(FetchMode.JOIN)
@ManyToOne(fetch = FetchType.LAZY)
at the same time throw away LAZY loading feature of hibernate ?
 
Hemant Thard
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

See this is what the document says about join = fetch

With fetch="join" on a collection or single-valued association mapping, you will actually avoid the second SELECT (hence making the association or collection non-lazy), by using just one "bigger" outer (for nullable many-to-one foreign keys and collections) or inner (for not-null many-to-one foreign keys) join SELECT to get both the owning entity and the referenced entity or collection. If you use fetch="join" for more than one collection role for a particular entity instance (in "parallel"), you create a Cartesian product (also called cross join) and two (lazy or non-lazy) SELECT would probably be faster.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic