Entity with Natural ID not being cached in the persistenceContext, causing extra queries

Description

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:

and

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.

Attachments

1

Activity

Vlad MihalceaMay 8, 2017 at 1:45 PM

Applied PR upstream.

Joao Carlos Heringer MoreiraApril 27, 2017 at 10:36 PM

Fixed

Details

Assignee

Reporter

Components

Fix versions

Affects versions

Priority

Created April 27, 2017 at 8:42 PM
Updated September 13, 2017 at 6:25 PM
Resolved May 8, 2017 at 1:45 PM