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?).