HHH-6625 : EntityPrinter.toString() fails for non-POJO entities (happens when logging)

This commit is contained in:
Gail Badner 2011-09-22 12:53:40 -07:00
parent e20a0e6a9f
commit d56ebfb443
2 changed files with 40 additions and 29 deletions

View File

@ -110,25 +110,33 @@ public abstract class AbstractFlushingEventListener implements Serializable {
}
//some statistics
if (LOG.isDebugEnabled()) {
LOG.debugf(
"Flushed: %s insertions, %s updates, %s deletions to %s objects",
session.getActionQueue().numberOfInsertions(),
session.getActionQueue().numberOfUpdates(),
session.getActionQueue().numberOfDeletions(),
persistenceContext.getEntityEntries().size()
);
LOG.debugf(
"Flushed: %s (re)creations, %s updates, %s removals to %s collections",
session.getActionQueue().numberOfCollectionCreations(),
session.getActionQueue().numberOfCollectionUpdates(),
session.getActionQueue().numberOfCollectionRemovals(),
persistenceContext.getCollectionEntries().size()
);
new EntityPrinter( session.getFactory() ).toString(
persistenceContext.getEntitiesByKey().values().iterator()
);
logFlushResults( event );
}
@SuppressWarnings( value = {"unchecked"} )
private void logFlushResults(FlushEvent event) {
if ( ! LOG.isDebugEnabled()) {
return;
}
final EventSource session = event.getSession();
final PersistenceContext persistenceContext = session.getPersistenceContext();
LOG.debugf(
"Flushed: %s insertions, %s updates, %s deletions to %s objects",
session.getActionQueue().numberOfInsertions(),
session.getActionQueue().numberOfUpdates(),
session.getActionQueue().numberOfDeletions(),
persistenceContext.getEntityEntries().size()
);
LOG.debugf(
"Flushed: %s (re)creations, %s updates, %s removals to %s collections",
session.getActionQueue().numberOfCollectionCreations(),
session.getActionQueue().numberOfCollectionUpdates(),
session.getActionQueue().numberOfCollectionRemovals(),
persistenceContext.getCollectionEntries().size()
);
new EntityPrinter( session.getFactory() ).toString(
persistenceContext.getEntitiesByKey().entrySet()
);
}
/**

View File

@ -25,13 +25,13 @@
package org.hibernate.internal.util;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.jboss.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.bytecode.instrumentation.spi.LazyPropertyInitializer;
import org.hibernate.engine.spi.EntityKey;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.TypedValue;
import org.hibernate.internal.CoreMessageLogger;
@ -39,7 +39,7 @@ import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.type.Type;
/**
* Renders entities to a nicely readable string.
* Renders entities and query parameters to a nicely readable string.
* @author Gavin King
*/
public final class EntityPrinter {
@ -49,12 +49,14 @@ public final class EntityPrinter {
private SessionFactoryImplementor factory;
/**
* Renders an entity to a string.
*
* @param entityName the entity name
* @param entity an actual entity object, not a proxy!
* @return the entity rendered to a string
*/
public String toString(Object entity) throws HibernateException {
// todo : this call will not work for anything other than pojos!
// need a means to access the entity name resolver(s). problem is accounting for session interceptor from here...
EntityPersister entityPersister = factory.getEntityPersister( entity.getClass().getName() );
public String toString(String entityName, Object entity) throws HibernateException {
EntityPersister entityPersister = factory.getEntityPersister( entityName );
if ( entityPersister == null ) {
return entity.getClass().getName();
@ -80,7 +82,7 @@ public final class EntityPrinter {
result.put( names[i], strValue );
}
}
return entityPersister.getEntityName() + result.toString();
return entityName + result.toString();
}
public String toString(Type[] types, Object[] values) throws HibernateException {
@ -106,16 +108,17 @@ public final class EntityPrinter {
return result.toString();
}
public void toString(Iterator iterator) throws HibernateException {
if (!LOG.isDebugEnabled() || !iterator.hasNext()) return;
// Cannot use Map as an argument because it clashes with the previous method (due to type erasure)
public void toString(Iterable<Map.Entry<EntityKey,Object>> entitiesByEntityKey) throws HibernateException {
if ( ! LOG.isDebugEnabled() || ! entitiesByEntityKey.iterator().hasNext() ) return;
LOG.debugf( "Listing entities:" );
int i=0;
while ( iterator.hasNext() ) {
for ( Map.Entry<EntityKey,Object> entityKeyAndEntity : entitiesByEntityKey ) {
if (i++>20) {
LOG.debugf("More......");
break;
}
LOG.debugf( toString( iterator.next() ) );
LOG.debugf( toString( entityKeyAndEntity.getKey().getEntityName(), entityKeyAndEntity.getValue() ) );
}
}