Apologies, if I have cross-posted this. I thought I posted it yesterday, but now I can't find it.
I think Firebird is playing games.
Anyway, I'm trying to reimplement the AuditInfo Custom Type pattern to be a bit more useful in our application.
http://www.hibernate.org/48.html
The problem is that even when the AuditInfo is a CompositeUserType, Hibernate doesn't know about the relationship between the createdBy column of the entity and the application's User table.
What this means is that when you actually want to display attributes of the creating user of an entity to the client, you can't query for them directly.
You either have to iterate across a collection calling
load(User.class, entity.getAuditInfo().getCreatedBy()), or manually join the entity table and the user table and use some of Hibernates more esoteric projection features.
Both approaches are pretty unsatisfactory.
What I want to do is extend the AuditInfo/Interceptor pattern so that the createdBy/updatedBy attributes are modelled as associations to User objects (a normally mapped object in our application) instead of as Long objects.
The problem is that I can't see how to write the CompositeUserType.nullSafeGet() method that would need to construct the User objects.
The User class in our application is mapped as lazy, and I would definitely want the semantics of that to be obeyed by the UserType. I don't want to always be joining to the user table when it's not necessary, but similarly I would want to take advantage of the FETCH keyword when it is used.
Can anyone given me some tips on where to look in the doco?
Or are there maybe some examples? I looked in the "eg" and "test" directories but couldn't find anything that seemed relevant.
If there's no doco or examples, can anyone point me to the areas of the API I need for the following:
- figuring out whether the User object should be eagerly or lazily fetched
- recreating a normally mapped entity class (User) from within the nullSafeGet() method
The more I think about it, I don't understand how Hibernate will know how to construct the SQL query when a user is trying to read normal entities and eagerly fetch the audit-related User. I looked at the SessionImplementor interface, but that didn't seem to provide anything that would tell me whether I was eagerly or lazily loading the User objects.
Please let me know if what I am trying to do is not possible so I can figure out some other solution for getting the attributes of the audit-related users.