java - Hibernate: how to get a list of all the objects currently in the session -


i'm getting good, old , dreaded transientobjectexception, and, happens in such case, i'm having problems locating kind of subtle bug in code causing problem.

my question is: there way obtain list of every object that's in current hibernate session?

i'll have solved current problem time answer question, but, anyway, being able list session lot in next time happens.

hibernate not expose internals public, won't find searching in public api. can find answer in implementation classes of hibernate interfaces: method (taken http://code.google.com/p/bo2/source/browse/trunk/bo2implhibernate/main/gr/interamerican/bo2/impl/open/hibernate/hibernatebo2utils.java) tell if object exists in session:

public static object getfromsession         (serializable identifier, class<?> clazz, session s) {                   string entityname = clazz.getname();     if(identifier == null) {        return null;     }           sessionimplementor sessionimpl = (sessionimplementor) s;     entitypersister entitypersister = sessionimpl.getfactory().getentitypersister(entityname);     persistencecontext persistencecontext = sessionimpl.getpersistencecontext();     entitykey entitykey = new entitykey(identifier, entitypersister, entitymode.pojo);     object entity = persistencecontext.getentity(entitykey);     return entity;     } 

if drill down little more, see implementation of persistencecontext org.hibernate.engine.statefulpersistencecontext. class has following collections:

// loaded entity instances, entitykey private map entitiesbykey;  // loaded entity instances, entityuniquekey private map entitiesbyuniquekey;  // identity map of entityentry instances, entity instance private map entityentries;  // entity proxies, entitykey private map proxiesbykey;  // snapshots of current database state entities // have *not* been loaded private map entitysnapshotsbykey;  // identity map of array holder arrayholder instances, array instance private map arrayholders;  // identity map of collectionentry instances, collection wrapper private map collectionentries;  // collection wrappers, collectionkey private map collectionsbykey; //key=collectionkey, value=persistentcollection  // set of entitykeys of deleted objects private hashset nullifiableentitykeys;  // properties have tried load, , not found in database private hashset nullassociations;  // list of collection wrappers instantiating during result set // processing, need initialize @ end of query private list nonlazycollections;  // container collections load when owning entity not // yet loaded ... now, purely transient! private map unownedcollections;  // parent entities cache child cascading // may empty or not contains relation  private map parentsbychild; 

so, need cast persistencecontext statefulpersistencecontext, use reflection private collection want , iterate on it.

i suggest on debugging code. not public api , brake newer releases of hibernate.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -