• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

i not wnat to lazy initialization

 
Mark Kafe
Ranch Hand
Posts: 60
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i not use lazy initialization false, any other way that gives me this solutions for collections???
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you can set the fetch stategry in queries.
 
Mark Kafe
Ranch Hand
Posts: 60
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please gives me any idea? How can i set
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i not want to use lazy collection. can i use
this


???
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eduardo Bueno
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic