I have been moving a project from JPA to Hibernate native implementation (got outvoted by team :<).
One of the fabulous things I have to deal with is that @PostLoad does not work with Hibernate native library. So I Googled a bit.
First I looked into EventListeners -> Nope, the Configuration class no longer seems to let you set it. And when I injected it in the cfg file, the listeners still refuse to work.
Okay, next, I looked into Interceptors -> Nope, no post-load. instantiate also doesn’t pass in the object itself. How convenient.
And then finally, I looked into LifeCycle -> Wow yes, it has onLoad(), but it passes in the Session and a Serializable Id but no object. Nice.
Anyway, all I want is something that listens for post-load with a handler function called onPostLoad(Object object, yadda yadda yadda). Does anyone have a good suggestion?
Interceptors do have an
onLoad()method. Have you checked into that? Its signature looks like this:Note that if you change a property in the entity, you will have to return true, and return false otherwise (say you are simply counting entities, for example).
If you are using Spring, you will wire your interceptor in like this: