|
This is a really irritating gotcha. I am trying to implement consistent paging for REST results, via timestamp. Envers seemed great for this, just select the revision based on the time stamp, and then AuditQuery for the revision... supplying maxResults and firstResult as needed. Feed that to Jackson poof a pretty json version of what's in my DB at the time of the initial query for as many next/previous page results as I want.
Unfortunately, this issue totally screws me because every collection is lazy, an Jackson nearly always gives me this:
Caused by: org.hibernate.SessionException: Session is closed! at org.hibernate.internal.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:129) at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1776) at org.hibernate.envers.entities.mapper.relation.query.AbstractRelationQueryGenerator.getQuery(AbstractRelationQueryGenerator.java:70) at org.hibernate.envers.entities.mapper.relation.lazy.initializor.AbstractCollectionInitializor.initialize(AbstractCollectionInitializor.java:62) at org.hibernate.envers.entities.mapper.relation.lazy.proxy.CollectionProxy.checkInit(CollectionProxy.java:49) at org.hibernate.envers.entities.mapper.relation.lazy.proxy.CollectionProxy.iterator(CollectionProxy.java:69) at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:90)
It took me two days to figure out that this problem was due to envers, not due to my hibernate mappings. Since it is clearly trying to fetch a collection and failing and that makes it look like something has been done wrong with the mappings. (I must have read EVERY stack overflow post mentioning FetchType.EAGER, @Fetch etc and of course none of them worked).
Please fix this.
Until then, please update your docs. Knowing that the mappings will behave critically differently from regular usage is really key for folks when they try to design an application. Now I have to either completely re-design, or hack around this. One of my attempts before I found this issue referenced on stack overflow was to add callbacks which mysteriously didnt get called, but I'm suddenly guessing that that is not supported either. The docs spend a lot of time talking about what envers can do, but they fail to clearly describe it's limitations. Until envers audit queries are as capable as regular queries, and respect all the mapping features please add a section called Limitations (the "mapping exceptions" section title is cryptic, and this problem isn't even mentioned there. I found one paragraph that mentions not traversing relations). Consolidating the information about the limitations will save people days of time, and possibly keep them from loosing clients by basing their design on what they initially think envers can do but later discover it lacks.
|