• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

i not wnat to lazy initialization

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


???
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic