-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Lazy=true is ignored
PostPosted: Wed Mar 09, 2016 6:24 pm 
Newbie

Joined: Sun Feb 28, 2016 3:16 pm
Posts: 17
Sorry for same theme again, but I additionally investigated it.
https://hibernate.atlassian.net/browse/HHH-10604
I added session closing to code from this issue, removed all Hibernate.initialize() calls, set lazy to true,
checked with reflection (not getter!) and the children where not empty!!!
Why? This makes me crazy...
What is the simpliest way to disable this with mapping or other?
Thank you a lot!


Top
 Profile  
 
 Post subject: Re: Lazy=true is ignored
PostPosted: Thu Mar 10, 2016 1:59 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Calling a method directly or through Reflection is going to initialize the association.
To prove this, I modified your test to this:

Code:
@Test
public void testGetByParentIdLazy() throws Exception {
    CategoryDAO<Category> categoryCategoryDAO = new CategorySqlHibernateDAO();
    prepare();

    List<Category> result = categoryCategoryDAO.getByParentIdLazy(0);
    Assert.assertTrue(result.size() == 1);
    Category category = result.get( 0 );
    System.out.println("Fetch by reflection");
    Object children = category.getClass().getMethod( "getChildren" ).invoke( category );
    System.out.println(children);
}


and the output is:

Code:
Hibernate:
    select
        category0_.categoryId as category1_0_,
        category0_.categoryName as category2_0_,
        category0_.categoryImage as category3_0_,
        category0_.categoryParentId as category4_0_
    from
        Category category0_
    where
        categoryParentId=?
Fetch by reflection
Hibernate:
    select
        children0_.categoryParentId as category4_0_0_,
        children0_.categoryId as category1_0_0_,
        children0_.categoryId as category1_0_1_,
        children0_.categoryName as category2_0_1_,
        children0_.categoryImage as category3_0_1_,
        children0_.categoryParentId as category4_0_1_
    from
        Category children0_
    where
        children0_.categoryParentId=?
[com.electronic.commerce.models.Category@8cfb95e3]


So,

1. The Category select is executed and the children is not initialized
2. Then comes the "Fetch by reflection"
3. We access the children through reflection and Hibernate generates the child select so initialize the lazy collection

N.B. When it comes to initializing Proxies, you need to use logging because debugging will always cause the Proxy to be initialized since the IDE tries to analyze the state of all fields.


Top
 Profile  
 
 Post subject: Re: Lazy=true is ignored
PostPosted: Thu Mar 10, 2016 5:19 am 
Newbie

Joined: Sun Feb 28, 2016 3:16 pm
Posts: 17
Thank you again for reply!
I got children with instance field access, not method, without debugging.
So IDE or JUnit were not asked to call getter.

Are there any ways to DISABLE this lazy init stuff without cloning of proxy to new not proxy object?
THANK you very much!


Top
 Profile  
 
 Post subject: Re: Lazy=true is ignored
PostPosted: Thu Mar 10, 2016 6:09 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
For collections, you have two options: lazy or eager.
If you want ultimate flexibility, you need to remove the collection altogether and replace it with a query that's called in the service layer.
This way, you execute the query explicitly and you gain full control.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.