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.  [ 2 posts ] 
Author Message
 Post subject: NaturalId Second Level Cache Support in HBM files
PostPosted: Mon Apr 18, 2016 3:19 am 

Joined: Thu Feb 26, 2004 11:45 am
Posts: 46
Currently using
Hibernate 4.3.6 Final
NON-JPA ... using hibernate mapping files. No annotations

NaturalID lookup support for Second Level Cache...

It appears (looking at Hibernate L2 Statistics) that this is supported differently in Hibernate 4 vs 3, and presumably more efficiently in Hibernate 4.
However, we have been unable to utilize it.

Mapping is straightforward:
<class name="com.trivin.bo.CodedValue" discriminator-value="0" mutable="true" dynamic-update="false" table="CODED_VALUES">
    <cache usage="read-write" />

    <id name="codedValueID" column="VALUE_ID" type="long">
      <generator class="native" />
    <discriminator column="TYPE_ID" type="integer" force="false" insert="false" not-null="true" />

    <natural-id mutable="false">
      <property name="productID" column="PRODUCTID" />
      <property name="typeID" type="integer" column="TYPE_ID" />
      <property name="code" column="CODE" length="30" />
    <property name="description" column="DESCRIPTION" length="100" />

When interrogating the Hibernate Configuration at runtime, the "naturalIdCacheRegionName" is null. Not sure if that is relevant.

After many calls using Session.byNaturalId(...) we do not see any caching taking place in L2, querying and viewing the cache.

Articles on the net seem to indicate that this is turned on via an Annotation. I do not see any other setting in the Hibernate Mapping file that might correspond to this.

Can somebody confirm that this support is or is not available without annotating? If so, is this still the case in Hibernate 5?
We just do not want to keep chasing down something that may not be supported.

No, we cannot move to annotate our hundreds and hundreds of classes at this point, as it would require much work, testing, etc.

As a side note, we could do the lookup using straight basic criteria lookup with query cache on, we do see caching taking place with this approach. But, we'd like to eliminate query cache, and we are seeing issues with query cache/filter clashes with duplicates generated in L2 cache.

thanks for any input or advice.

 Post subject: Re: NaturalId Second Level Cache Support ...
PostPosted: Mon Apr 18, 2016 7:53 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
The Hibernate 4.0 XML schema does not define a natural-id-cache, so this feature is not supported for HBM files.

You can mix HBM and annotations. For this you need to use an embeddable where you add all the columns that form the natural id, as indicated in the 5.0 docs.

Then your entity class should look like this:

public class CodedValue {

    @Column(name = "PRODUCTID")
    private Long productID;
    @Column(name = "TYPE_ID")
    private Integer typeID;
    @Column(name = "CODE", length = 30)
    private String code;

So, you are only changing the properties that are relevant for the natural id mapping.

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