This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes i not wnat to lazy initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "i not wnat to lazy initialization" Watch "i not wnat to lazy initialization" New topic
Author

i not wnat to lazy initialization

Mark Kafe
Ranch Hand

Joined: Jun 23, 2008
Posts: 60

if i not use lazy initialization false, any other way that gives me this solutions for collections???
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Yes, you can set the fetch stategry in queries.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Mark Kafe
Ranch Hand

Joined: Jun 23, 2008
Posts: 60

please gives me any idea? How can i set
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

I'm not sure I understand what you are asking. Are you unsure how to define a fetch strategy in a query?
Eduardo Bueno
Ranch Hand

Joined: Jun 04, 2009
Posts: 155
You don't want to use lazy initialized collections? Then just define them as FetchType.EAGER. If you want a better approach, you can use them lazy implementing OpenSessionInView pattern, which will load them once you try to access it in code. I don't know if it can be implemented without hibernate, though.
Mark Kafe
Ranch Hand

Joined: Jun 23, 2008
Posts: 60

yes i not want to use lazy collection. can i use
this


???
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Yes, though you are still using lazy initialization there you are just manually forcing it to initialize. So its no different from just accessing a property on the lazily loaded association, because that will cause the proxy to be initialized. If you know you want the associated object then I'd suggest you eagerly fetch it as part of your query.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Really, if you need the associated objects, fetch them eagerly, if you don't, leave them with the default of being loaded lazily. But dont' make them lazy if you KNOW that you are going to be fetching them anyways.

-Cameron McKenzie
Benjamin Winterberg
Ranch Hand

Joined: Sep 19, 2007
Posts: 36
Eduardo Bueno wrote:You don't want to use lazy initialized collections? Then just define them as FetchType.EAGER. If you want a better approach, you can use them lazy implementing OpenSessionInView pattern, which will load them once you try to access it in code. I don't know if it can be implemented without hibernate, though.


FetchType.EAGER is fine when loading entities via Session#get(), but its ignored when using HQL or Criteria.

If you dont want lazy collections on entites loaded by Criteria or HQL you have to explicitly JOIN FETCH them.


Hibernate DAOs with Spring | Separation of Concerns | Eclipse Code Templates | more...
Eduardo Bueno
Ranch Hand

Joined: Jun 04, 2009
Posts: 155
ben win wrote:
Eduardo Bueno wrote:You don't want to use lazy initialized collections? Then just define them as FetchType.EAGER. If you want a better approach, you can use them lazy implementing OpenSessionInView pattern, which will load them once you try to access it in code. I don't know if it can be implemented without hibernate, though.


FetchType.EAGER is fine when loading entities via Session#get(), but its ignored when using HQL or Criteria.

If you dont want lazy collections on entites loaded by Criteria or HQL you have to explicitly JOIN FETCH them.

That is not true, the only way objects are not fetched is with plain SQL.
Benjamin Winterberg
Ranch Hand

Joined: Sep 19, 2007
Posts: 36
Eduardo Bueno wrote:
ben win wrote:
Eduardo Bueno wrote:You don't want to use lazy initialized collections? Then just define them as FetchType.EAGER. If you want a better approach, you can use them lazy implementing OpenSessionInView pattern, which will load them once you try to access it in code. I don't know if it can be implemented without hibernate, though.


FetchType.EAGER is fine when loading entities via Session#get(), but its ignored when using HQL or Criteria.

If you dont want lazy collections on entites loaded by Criteria or HQL you have to explicitly JOIN FETCH them.

That is not true, the only way objects are not fetched is with plain SQL.


When you use FetchType.EAGER with HQL or Criteria the generated SQL does not contain the specified eager fetching. Instead you should use FETCH JOINs to reduce the database statements to 1 SELECT.
Eduardo Bueno
Ranch Hand

Joined: Jun 04, 2009
Posts: 155
That has nothing to do with what you said before. Just because hibernate generates more than one SELECT command doesn't mean the entity will not be fetched.
Benjamin Winterberg
Ranch Hand

Joined: Sep 19, 2007
Posts: 36
I was refering to this: https://www.hibernate.org/117.html#A13

But maybe I was wrong about EAGER fetching by SELECT.


No matter what, best practices goes around lazy fetching:

Prefer lazy fetching for associations.
Use eager fetching sparingly. Use proxies and lazy collections for most associations to classes that are not
likely to be completely held in the second-level cache. For associations to cached classes, where there is an
a extremely high probability of a cache hit, explicitly disable eager fetching using lazy="false". When an
join fetching is appropriate to a particular use case, use a query with a left join fetch.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: i not wnat to lazy initialization
 
Similar Threads
what is lazy initialization in hibernate
Doubt in Effective Java
Lazy Initialization
No need for synchronization; there is "Lazy Initialization"
New java singleton pattern implementation