-->
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: Collection and second level cache
PostPosted: Mon Nov 30, 2015 6:16 pm 
Newbie

Joined: Mon Nov 30, 2015 5:45 pm
Posts: 1
Hi to all,

I'm writing for ask a clarification about the expected behavior of collection when second level cache is enabled.
Suppose a simple entity Item with a collection of Bids.
Item:
Int32 Id;
IList<Bid> bids;

My question is the following: when I persist entity Item, the associated collection bids (both collection id and bid items) it’s inserted on second level cache or it’s inserted next time the collection is accessed?
If I open a new Hibernate session and perform a get by Id of Item just inserted, are the entire Item and Bibs get from cache or it’s require a query on database?

Do you know if there is some specification about following behavior.

Thank you very much
Maurizio


Top
 Profile  
 
 Post subject: Re: Collection and second level cache
PostPosted: Tue Dec 01, 2015 9:50 am 
Regular
Regular

Joined: Mon Oct 19, 2015 7:49 am
Posts: 61
Location: ChengDu China
Hi maurizio.80

Hiberante supports 2 caches: Second level cache and Query cache.

(1) Second level cache is simple, it can only help you to query object by id, but it is very helpful for many-to-one relationship, that's why @javax.persistence.ManyToOne dares to use FetchType.EAGER to be its default value.

(2) Query cache is complex cache, it can help you to query object by complex SQL, maybe it's smart enough so that it's helpful for the collection query. try to validate it by yourself please:)


Top
 Profile  
 
 Post subject: Re: Collection and second level cache
PostPosted: Thu Dec 03, 2015 2:43 am 
Beginner
Beginner

Joined: Thu Nov 26, 2015 11:40 am
Posts: 33
I'm assuming that you have also enabled second level cache on Bid class as well although you have specified in your post.

The second level cache gets populated when the session tries to fetch the data from DB. So in your case the initial query to load the Bid after insertion should result in DB hit. But the subsequent fetches should result in second level cache hit. Are you noticing any different behavior?

By the way, if you have not enabled second level cache on both Bid class it would result in DB hit always unless it is in a same session.

-Madhu.


Top
 Profile  
 
 Post subject: Re: Collection and second level cache
PostPosted: Thu Dec 03, 2015 3:20 am 
Regular
Regular

Joined: Mon Oct 19, 2015 7:49 am
Posts: 61
Location: ChengDu China
Yes, I was noticing different behaviors of those two caches.

(1) 2nd level cache can only optimize the simplest SQL like this
Code:
select * from tableName where primaryKey = ?

This is useful for
* org.hibernate.Session.get,
* org.hibernate.Session.load,
* javax.persistence.EntityManager.find,
* javax.persistence.ENtityManager.load
* The loading of many-to-one property, thats why I said it's the reason why "@javax.persistence.ManyToOne" dares to use FetchType.EAGER to be its default value.

(2) Query Cache, it's more complex and more smart, because it's designed for complex SQL,
For example, if you want to load child object,
* if collection property is one-to-many, the collection loading SQL should be
Code:
select * from childTable where foreignKeyToParentTable = ?

* if collection property is many-to-many, the collection loading SQL should be
Code:
select * from thisTable
inner join middleTable on thisTable.primaryKey = middleTable.foreignKeyToThis
where middleTable.foreignKeyToThat = ?

These SQLs are more complex, so only Query cache can help you.


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.