Hi all,
I'm looking at using Hibernate to implement a scenario where I'm aggressively caching part of the object graph. I've been going through the documentation on things like custom types, but I'd very much appreciate any suggestions that you might have for how to best achieve my goal.
My domain model basically looks like
Code:
class A {
String id;
B b;
}
class B {
String id;
}
with the obvious underlying datamodel where the A table contains a foreign key referring to the id column of the B table; IOW a many-to-one relation between A and B.
I would now like to preload all of the B instances into a map from B-id to B-instance, so when I'm loading an A instance from the database I can resolve the B reference by just looking up the B instance in the map. Corresponding to this, when I'm updating or creating an A instance I would like to just retrieve the id value of the associated B instance and store that in the underlying column without verifying the existence of the B key in the database.
Later on I will also need this to work going in the other direction where B has a collection of A's and the A instances are retrieved from a map.
My immediate thought was to implement a custom type for this purpose, but as that's intended for value types only and not entities I'm not sure if it will work, especially when it comes to collection mapping. Is it necessary to create a custom persister instead, and would that even be possible given the requirement that the initial preload from the database should still work?
Best regards & thanks,
Mikkel Lauritsen