Mazhar
2018-04-04 20:40:12 UTC
Hello All,
We have couple of one-to-one relations mapped with primary key. The
optional part is mapped with constraint="true" and this object is being
eager fetched along with primary object and that part is working fine. The
problem happens when we enable level 2 caching on both entities. In this
case every cache hit for main entity leads to SELECT for optional entity
which doesn't exit.
Product => ProductId, Name,....
Kit => ProductId, SomeField
Product and Kit share same primary key where only some products can be Kit
making it Kit optional in this one-to-one relation. After reading
documentation and going through different discussions on stack overflow It
turned out that a less hectic way to implement one-to-one relationship is
by using foreign key and unique key constraint. This would not only enable
lazy loading but also help level 2 cache take care of optional Kit entity.
I had to adjust the schema in following way
Product => ProductId, Name, KitId
Kit => KitId, SomeField
where Product now has KitId field which foreign key from Kit table, In
mappings product looks at kit using many-to-one with unique=true while kit
has its property-ref set to "Kit"
Level 2 cache is working but I am confused about one thing.
In profiler I can see that if I just check product.kit.SomeField value it
registers a select for its related product which is already in cache. This
seems to be happening due to property-ref="Kit' which basically seems to
tell that *select * from Product where KitId=32* and because cache only
works for direct Id loads hence this results in cache miss. If I remove
property-ref then nhiberate try to use KitId value to load product and
fails.
My question is can I remove this extra SELECT and instead make it use
already loaded product from cache? One idea is to keep using shared primary
key and remove property-ref attribute but that may create circular
relations and issues with cascades.
Have I mapped it correctly?
Regards
We have couple of one-to-one relations mapped with primary key. The
optional part is mapped with constraint="true" and this object is being
eager fetched along with primary object and that part is working fine. The
problem happens when we enable level 2 caching on both entities. In this
case every cache hit for main entity leads to SELECT for optional entity
which doesn't exit.
Product => ProductId, Name,....
Kit => ProductId, SomeField
Product and Kit share same primary key where only some products can be Kit
making it Kit optional in this one-to-one relation. After reading
documentation and going through different discussions on stack overflow It
turned out that a less hectic way to implement one-to-one relationship is
by using foreign key and unique key constraint. This would not only enable
lazy loading but also help level 2 cache take care of optional Kit entity.
I had to adjust the schema in following way
Product => ProductId, Name, KitId
Kit => KitId, SomeField
where Product now has KitId field which foreign key from Kit table, In
mappings product looks at kit using many-to-one with unique=true while kit
has its property-ref set to "Kit"
Level 2 cache is working but I am confused about one thing.
In profiler I can see that if I just check product.kit.SomeField value it
registers a select for its related product which is already in cache. This
seems to be happening due to property-ref="Kit' which basically seems to
tell that *select * from Product where KitId=32* and because cache only
works for direct Id loads hence this results in cache miss. If I remove
property-ref then nhiberate try to use KitId value to load product and
fails.
My question is can I remove this extra SELECT and instead make it use
already loaded product from cache? One idea is to keep using shared primary
key and remove property-ref attribute but that may create circular
relations and issues with cascades.
Have I mapped it correctly?
Regards
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.