-->
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.  [ 7 posts ] 
Author Message
 Post subject: NaturalIds and caching
PostPosted: Wed Jun 22, 2016 1:21 pm 
Newbie

Joined: Wed Jun 22, 2016 1:17 pm
Posts: 7
In our legacy database we have a lookup table like the folowing:

Code:
CREATE TABLE LOOKUP_TABLE (
  ID   INTEGER   NOT NULL,
  CODE   SMALLINT   NOT NULL,
  ITEM   SMALLINT   NOT NULL,
  DESCRIPTION   CHARACTER(100)
  )


which is referred in other tables like:

Code:
CREATE TABLE ENTITY (
  ID   INTEGER   NOT NULL
  PROP_CODE   SMALLINT,
  PROP_ITEM   SMALLINT
  )


Im trying to map those to :

Code:
@Entity
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
@NaturalIdCache
@Immutable
@Table(name="LOOKUP_TABLE")
public class LookupTable implements Serializable{

   private static final long serialVersionUID = 1L;

   @Id
   @Column
   protected Integer id;

   @NaturalId(mutable=false)
   @Column
   protected Integer code;
   
   @NaturalId(mutable=false)
   @Column
   protected Integer item;
   
   @Column
   protected String description;

...
}


and:
Code:
@Entity
@Table(name="ENTITY")
public class Entity implements Serializable {
   
   private static final long serialVersionUID = 3351554143731839754L;

   @Id
   @Column
   private Integer id;
   
   @ManyToOne(fetch=FetchType.LAZY)
   @JoinColumns({
         @JoinColumn(name="PROP_CODE", referencedColumnName="CODE"),
         @JoinColumn(name="PROP_ITEM", referencedColumnName="ITEM")})
   private LookupTable property;

...

}


On application startup, second level cache for LookupTable naturalId seems to be created, but when I get entityA and entityB, both with property poiting to the same lookuptable, and do entityA.getProperty() followed by entityB.getProperty(), hibernate shoots the same query twice.

My understanding is that is that entityA.getProperty() should shoot the query, and entityB.getProperty() should get the result from the session cache, or at least from the second level cache, is that correct?
If that is correct, how can i fix that? Is there any other way to make that mapping that is more "correct" for hibernate(i can not modify the schema)?

Im using hibernate 5.1.0.Final, with spring data (could it be a problem with spring data?).


Top
 Profile  
 
 Post subject: Re: NaturalIds and caching
PostPosted: Thu Jun 23, 2016 3:10 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Try to provide a replicating test case, and then open a JIRA issue for this prolem.


Top
 Profile  
 
 Post subject: Re: NaturalIds and caching
PostPosted: Fri Jun 24, 2016 10:30 am 
Newbie

Joined: Wed Jun 22, 2016 1:17 pm
Posts: 7
mihalcea_vlad wrote:
Try to provide a replicating test case, and then open a JIRA issue for this prolem.


Hi Vlad, thanks for the response!

I've tried to make a test case to show the issue, but since i am far from good with hibernate, could you please check first if my assumptions are correct, before i open an issue?

The test was made using hibernate's test templates, and is avaliable at https://github.com/jcmoreira/hibernate-bug-test-case


Top
 Profile  
 
 Post subject: Re: NaturalIds and caching
PostPosted: Fri Jun 24, 2016 1:41 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Sure, I'm going to take a look on Monday.


Top
 Profile  
 
 Post subject: Re: NaturalIds and caching
PostPosted: Mon Jun 27, 2016 8:12 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
I checked your 2 test cases:

1. First, the natural-id cache is not read-through as you assert.

2. Second, the natural-id cache only when you load by natural-id. In this particular use case, you initialize the Child entity (which by the way is a parent entity because the child has a FK back to a parent). So you don't load the Child entity by its natural id, but by an Embeddable type which happens to store the same keys as the natural id. There is no such feature implemented in Hibenrate, so you can at most add a new feature request.


Top
 Profile  
 
 Post subject: Re: NaturalIds and caching
PostPosted: Fri Jul 01, 2016 12:45 pm 
Newbie

Joined: Wed Jun 22, 2016 1:17 pm
Posts: 7
Thanks for the help Vlad!

Looking through hibernate's code, i believe my problem would be solved if the TODO at line:

https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/type/EntityType.java#L678

was implemented, am i corect ?

If so, are there any plans on implementing that ? Is there any workaround (some interceptor stuf, idk)?


Top
 Profile  
 
 Post subject: Re: NaturalIds and caching
PostPosted: Fri Jul 01, 2016 1:53 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You can open a Jira issue. Best if you come with a Pull Request too.


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