I try some experiment and maybe need some suggestions.
The idea behind my experiment is to get rid of LazyInitializationException and detached objects in an JSF (or webapp in general) application but still using the session-per-request pattern.
The only exception is StaleObjectException on version conflict - and this is wanted.
I found a way to wrap a entity using cglib and use this wrappped instance as replacement in my code. The wrapper acts like a "transfer object" and when it comes to save it automatically merges the object with the hibernate entity. This works witout great change in my code - only a one-liner to wrap the entity.
The problem I have is that this wrapper (while still have the same signature like to orignial entity just enhanced with cglib) cant be directly used in hibernate, in particular with the criteria api.
Code:
Caused by: org.hibernate.HibernateException: instance not of expected entity type: com.ops.Contact.model.po.Relation
at org.hibernate.persister.entity.BasicEntityPersister.getSubclassEntityPersister(BasicEntityPersister.java:3083)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1080)
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:221)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:107)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:72)
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1456)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1518)
at org.hibernate.loader.Loader.doQuery(Loader.java:638)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:221)
at org.hibernate.loader.Loader.doList(Loader.java:1959)
at org.hibernate.loader.Loader.list(Loader.java:1928)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1314)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:298)
I encapsulated all session methods so there I do not have a problem to "unwrap" my object. But I am not able to do the same with Expression et al.
So what I would like to know if there is a place where I can make a hook to unwrap my entity.
And if there is no place, is it possible to build one - aka Interceptor?
And if not, why is
org.hibernate.criterion.Expression final
and the constructor of org.hibernate.criterion.Restrictions package private.
If we could change this so that I can extend my own Expression class I could unwrap my entity there.
Thanks!