I'm a bit change forceLoad() method, for me it's looks like this:
Code:
protected SessionFactory getSessionFactory() {
Session session = (Session) entityManager.getDelegate();
return session.getSessionFactory();
}
public boolean isSkipClass(Class clazz) {
Class[] skippedClasses = new Class[]{
// classes to skip
};
return Arrays.asList(skippedClasses).contains(clazz);
}
protected void forceLoad(Object entity) throws HibernateException {
if (entity == null) {
return;
}
if (isSkipClass(entity.getClass())) {
return;
}
ClassMetadata classMetadata = getSessionFactory().getClassMetadata(
entity.getClass()
);
if (classMetadata == null) {
return;
}
Hibernate.initialize(entity);
for (int i = 0, n = classMetadata.getPropertyNames().length; i<n; i++) {
String propertyName = classMetadata.getPropertyNames()[i];
Type type = classMetadata.getPropertyType(propertyName);
if (type.isEntityType()) {
Object subEntity = classMetadata.getPropertyValue(entity, propertyName, EntityMode.POJO);
forceLoad(subEntity);
} if (type.isCollectionType()) {
Collection collection = (Collection) classMetadata.getPropertyValue(entity, propertyName, EntityMode.POJO);
if (collection != null && collection.size() > 0) {
for (Object collectionItem : collection) {
forceLoad(collectionItem);
}
}
}
}
}
Import section:Code:
import com.thoughtworks.xstream.XStream;
import org.hibernate.EntityMode;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.collection.PersistentBag;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.type.Type;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import javax.persistence.EntityManager;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;