-->
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: First level cached only on @Id column, right?
PostPosted: Wed Jul 14, 2010 7:04 am 
Newbie

Joined: Wed Jul 14, 2010 6:55 am
Posts: 2
Am I correct in the understanding, that hibernate Session cache/1. level cache only caches entities based on the PK-mapped column. In my case, because I use JPA, the @Id annotated property?

Reason I ask is, that I have mappings between entities on non-PK mapped entities (but they have a DB unique-constraint, so they ARE unique), and it would be über cool, if hibernate could cache on that value also. I use @JoinColumn to tell it which to join on.

The database table design is this:

TABLE parent (
ID INT (PK column),
OTHER_ID VARCHAR (UNQ, but not PK column)
)

TABLE child (
PARENT_OTHER_ID VARCHAR (FK to parent.OTHER_ID)
)

And I would like hibernate to cache "parent" instances based on BOTH "ID" and "OTHER_ID" in the 1. level cache.

While I wrote this up, it kind of sounded like impossible to me, but is there any cool stuff I need to know about that could help me out here? (aside from changing my datamodel, that is).


Top
 Profile  
 
 Post subject: Re: First level cached only on @Id column, right?
PostPosted: Mon Jul 19, 2010 7:39 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
First level cached only on @Id column, right?


Yes, that's right.
If you want to have cached elements by @NaturalId or relation collections
then you must consider to use the 2nd level cache.


Top
 Profile  
 
 Post subject: Re: First level cached only on @Id column, right?
PostPosted: Fri Jul 23, 2010 5:35 am 
Newbie

Joined: Wed Jul 14, 2010 6:55 am
Posts: 2
pb00067 wrote:
If you want to have cached elements by @NaturalId or relation collections
then you must consider to use the 2nd level cache.


It was my understanding that the second-level cache would only index on id too. Am I wrong in that?


Top
 Profile  
 
 Post subject: Re: First level cached only on @Id column, right?
PostPosted: Sat Jul 24, 2010 7:13 am 
Newbie

Joined: Thu Jul 15, 2010 6:44 am
Posts: 13
that's correct, second level cache will also index on @id.

However, I think you can use query cache, if you want to query out a collection using some other column than Id.

_________________
Vikram.
Http://enterprisejava4u.blogspot.com


Top
 Profile  
 
 Post subject: Re: First level cached only on @Id column, right?
PostPosted: Wed Aug 11, 2010 9:43 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
triforkpol wrote:
It was my understanding that the second-level cache would only index on id too. Am I wrong in that?


You are partially right and at same time wrong:
-if you only enable second-level cache without enabling second-level-query-cache, then the does index only on id.
-if you enable the second-level-query-cache too, then the cache can also index collections and query results.
So in order to cache lookup queries on @NaturalId you would have also enable the second-level-query-cache.


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.