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

后端 未结 2 443
陌清茗
陌清茗 2021-02-02 16:29

I\'m getting the good, old and dreaded TransientObjectException, and, as often happens in such case, I\'m having problems locating what kind of subtle bug in the co

2条回答
  •  无人共我
    2021-02-02 17:10

    Found @nakosspy post very useful. Inspired by his post, I added this very simple utility method that outputs the contents of Hibernate Session.

    As nakosspy said this is ONLY for debugging purposes as it is a HACK.

        public static void dumpHibernateSession(Session s) {
        try {
            SessionImplementor sessionImpl = (SessionImplementor) s;
            PersistenceContext persistenceContext = sessionImpl.getPersistenceContext();
            Field entityEntriesField = StatefulPersistenceContext.class.getDeclaredField("entityEntries");
            entityEntriesField.setAccessible(true);
            IdentityMap map = (IdentityMap) entityEntriesField.get(persistenceContext);
            log.info(map);
        } catch (Exception e)
        {
            log.error(e);
        }
    
    }
    

提交回复
热议问题