-->
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.  [ 5 posts ] 
Author Message
 Post subject: Final classes in Hibernate
PostPosted: Tue Apr 10, 2012 8:09 am 
Newbie

Joined: Tue Apr 10, 2012 7:55 am
Posts: 4
I have question about final classes in Hibernate. reading Hibernate documentation I have read:

Quote:
You can persist final classes that do not implement an interface with Hibernate; you will not, however, be able to use proxies for lazy association fetching which will ultimately limit your options for performance tuning.


Then I have developed a simple example, I have created two final classes Foo and Bar. Foo has a one to many relationship to Bar. Foo and Bar are annotated with @Entity and relationship with @OneToMany(mappedBy="foo", cascade={CascadeType.ALL}) and @ManyToOne, at field level access.

Then I create a test, which inserts a Foo and many Bar. Then I find by id a Foo object and then I call getBars() and it acts as a lazy, Hibernate does a query and returns all Bars.

What I am missing?, it is supposed to not create this query because classes are final, but query is executed.

Thank you very much for your help.


Top
 Profile  
 
 Post subject: Re: Final classes in Hibernate
PostPosted: Tue Apr 10, 2012 9:41 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi asotobu,

you seem to have misunderstood something.
At first thing we must distinct between lazy loading on collections (=ToMany relations) and
lazy loading on entity references (= ToOne relations).
Only the latter one uses proxies, so the difference turns out only in following scenario:

Code:
Session session = openNewSession();
Bar bar = session.get(Bar.class, barId);


In case that you declare class Foo non final,
hibernate can fetch the bar instance from database without need to read also its parent foo record,
because bar.foo will be filled with a proxy of Foo containing just the primary key information of that foo.
That proxy is a runtime subclass of class Foo.

In case that you declare class Foo final,
nobody is allowed to extend Foo, that means nobody can create subclasses of Foo, so hibernate cannot create a proxy for Foo and thus hibernate must load the parent foo instance eagerly. This results in having a second query being executed for fetching that foo record from database.

Hope now its clear.


Top
 Profile  
 
 Post subject: Re: Final classes in Hibernate
PostPosted: Tue Apr 10, 2012 9:48 am 
Newbie

Joined: Tue Apr 10, 2012 7:55 am
Posts: 4
Thank you very much for your answer, now I understand exactly what it means that line.

Thanks again,
Alex.


Top
 Profile  
 
 Post subject: Re: Final classes in Hibernate
PostPosted: Sun Apr 15, 2012 11:59 am 
Newbie

Joined: Tue Apr 10, 2012 7:55 am
Posts: 4
Hi, I am sorry to disturb you again, but I can't still watch any difference. I am using EntityManager find method to load an instance of Bar.

Code:
Bar bar = entityManager.find(Bar.class, id);


Foo is final (ManyToOne part) but not Bar.

I execute the code and a select from Bar with a left outer join to Foo is also executed. Seems normal because Foo is final an a proxy cannot be created. In fact if I debug, Foo is not a proxy.

Then I change Foo as not final, and the same behavior is seen.

Note that I am using JPA annotations, where by default ManyToOne is Eager.

Thank you very much again for your help.


Top
 Profile  
 
 Post subject: Re: Final classes in Hibernate
PostPosted: Mon Apr 16, 2012 2:51 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
Note that I am using JPA annotations, where by default ManyToOne is Eager.


Ok, you have already found the key to the explanation!

If you want to use lazy association fetching then you must declare
Code:
@javax.persistence.ManyToOne(fetch = FetchType.LAZY )

on your relation.
Only with lazy option set, hibernate will try to instantiate the associated entity as proxy instead to resolve it from db, OK?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.