Grails – how to see what objets are in Hibernate session
Sometimes, when your Grails controllers and services relationships get complex and you do bad stuff (even without explicitly doing it! you gotta love all this magic 😆 ), Hibernate throws you lovely exceptions like the following (this is one of the most common, but there are more of them):
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing
To make things even nicer, there are cases where this exceptions are thrown in code you don’t control (for example, a session flush done automatically by Hibernate when a transactional service method is invoked), so the stacktraces won’t help much (since none of the referenced classes that are yours has the line of the exception in there, remember, is autogenerated code 😀 ).
An approach for debugging this kind of errors is inspecting the session to see what objects are in there. You can do so in a service or a controller by accessing the current session. For example, the following code will search for objects in the session that are of class TypicalDomainClass:
class CoolController { def sessionFactory ... def fancyAction = { try { trickyStuffWithServices() } catch (throwable) { sessionFactory.currentSession.persistenceContext.entityEntries.findAll { it.key instanceof TypicalDomainClass }.each { log.debug "Found entity key: $it.key - $it.value" } } } }
In the typical case, the currentSession object is of class SessionImpl, while the persistenceContext is a StatefulPersistenceContext.
Parecidos razonables
-
Grails – Is my bean a singleton?
May 6, 2014
0 -
Grails – Hibernate-safe domain class property editor
July 22, 2014
0 -
Grails – my domain class instance says its class is abstract!
November 18, 2013
0 -
Grails – small and simple method to convert objects to JSON
January 12, 2013
0 -
Grails – generic methods for equals and hashCode calculation
December 27, 2012
3
Pingback: Grails – Hibernate-safe domain class property editor