File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes FetchType.LAZY and FetchMode.JOIN Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "FetchType.LAZY and FetchMode.JOIN" Watch "FetchType.LAZY and FetchMode.JOIN" New topic

FetchType.LAZY and FetchMode.JOIN

Yogesh Lonkar
Ranch Hand

Joined: Jul 17, 2012
Posts: 94

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
@ManyToOne(fetch = FetchType.LAZY)
at the same time throw away LAZY loading feature of hibernate ?

Learning some thing New Every Day
Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122

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.

I agree. Here's the link:
subject: FetchType.LAZY and FetchMode.JOIN
It's not a secret anymore!