| I am building an app connecting to a legacy database where almost all entities have properties linked to a lookup table representing their properties, so that the properties can have new values added at runtime by a generic mechanism. Since i can't change the schema, the way i found to map this situation is with these two sample classes:
@Entity
public class Property implements Serializable{
@Id
private Integer id;
@NaturalId
private Integer code;
@NaturalId
private Integer item;
...
}
and
@Entity
public class Entit implements Serializable{
@Id
private Integer id;
@ManyToOne
@JoinColumns({
@JoinColumn(name="PROP_CODE", referencedColumnName = "CODE"),
@JoinColumn(name="PROP_ITEM", referencedColumnName = "ITEM")
})
private Property prop;
...
}
It works, but i am having some performance problems when loading Entits referencing the same properties, even within a single session. My understanding is that if i load an Entit 1 that is linked to a Property 1 and and Entit 2 that is linked to the same Property 1, i would have, at most, 3 queries, one to load Entit 1, one to load Property 1 and then one to load Entit 2. When populating Entit 2, Property 1 should already be on the persistenceContext , so it should be retrieved there. Currently, my test shoots 4 queries. One to load Entit 1, one to load Property 1, one to load Entit 2 and then another one (repeated) to load Property 1 again. |