首页

关于hibernate-util源码包中HibernateUtil工具类获取SessionFactory、获取持久化类等常见操作

标签:hibernate-util,HibernateUtil工具类,SessionFactory     发布时间:2018-06-06   

一、前言

通过hibernate-util源码包中的net.sf.gilead.core.hibernate.HibernateUtil工具类,获取常用org.hibernate.SessionFactory工厂对象、打开session会话、获取持久化对象、初始化代理对象 Hibernate.initialize(proxy)等操作。

二、源码说明

1.HibernateUtil

package net.sf.gilead.core.hibernate;@b@@b@import java.io.Serializable;@b@import java.lang.reflect.Method;@b@import java.util.ArrayList;@b@import java.util.Collection;@b@import java.util.HashMap;@b@import java.util.Iterator;@b@import java.util.List;@b@import java.util.Map;@b@import java.util.Map.Entry;@b@import java.util.Set;@b@import java.util.SortedMap;@b@import java.util.SortedSet;@b@import net.sf.beanlib.hibernate.UnEnhancer;@b@import net.sf.gilead.core.IPersistenceUtil;@b@import net.sf.gilead.core.serialization.SerializableId;@b@import net.sf.gilead.exception.ComponentTypeException;@b@import net.sf.gilead.exception.NotPersistentObjectException;@b@import net.sf.gilead.exception.TransientObjectException;@b@import net.sf.gilead.pojo.base.IUserType;@b@import net.sf.gilead.util.IntrospectionHelper;@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@import org.hibernate.EntityMode;@b@import org.hibernate.Hibernate;@b@import org.hibernate.Session;@b@import org.hibernate.SessionFactory;@b@import org.hibernate.collection.AbstractPersistentCollection;@b@import org.hibernate.collection.PersistentBag;@b@import org.hibernate.collection.PersistentCollection;@b@import org.hibernate.collection.PersistentList;@b@import org.hibernate.collection.PersistentMap;@b@import org.hibernate.collection.PersistentSet;@b@import org.hibernate.collection.PersistentSortedMap;@b@import org.hibernate.collection.PersistentSortedSet;@b@import org.hibernate.engine.IdentifierValue;@b@import org.hibernate.impl.SessionFactoryImpl;@b@import org.hibernate.impl.SessionImpl;@b@import org.hibernate.metadata.ClassMetadata;@b@import org.hibernate.persister.collection.CollectionPersister;@b@import org.hibernate.persister.entity.EntityPersister;@b@import org.hibernate.proxy.HibernateProxy;@b@import org.hibernate.proxy.LazyInitializer;@b@import org.hibernate.tuple.IdentifierProperty;@b@import org.hibernate.tuple.entity.EntityMetamodel;@b@import org.hibernate.type.AbstractComponentType;@b@import org.hibernate.type.CollectionType;@b@import org.hibernate.type.Type;@b@@b@public class HibernateUtil@b@  implements IPersistenceUtil@b@{@b@  private static final String ID = "id";@b@  private static final String CLASS_NAME = "class";@b@  private static final String ROLE = "role";@b@  private static final String KEY = "key";@b@  private static final String ID_LIST = "idList";@b@  private static final String VALUE_LIST = "valueList";@b@  private static Log _log = LogFactory.getLog(HibernateUtil.class);@b@  private static HibernateUtil _instance = null;@b@  private SessionFactoryImpl _sessionFactory;@b@  private Map<Class<?>, Boolean> _persistenceMap;@b@  private Map<Class<?>, Class<?>> _unehancementMap;@b@  private ThreadLocal<Session> _session;@b@@b@  public static HibernateUtil getInstance()@b@  {@b@    if (_instance == null)@b@    {@b@      _instance = new HibernateUtil();@b@    }@b@    return _instance;@b@  }@b@@b@  public SessionFactory getSessionFactory()@b@  {@b@    return this._sessionFactory;@b@  }@b@@b@  public void setSessionFactory(SessionFactory sessionFactory)@b@  {@b@    if (!(sessionFactory instanceof SessionFactoryImpl))@b@    {@b@      sessionFactory = (SessionFactory)IntrospectionHelper.searchMember(SessionFactoryImpl.class, sessionFactory);@b@      if (sessionFactory == null)@b@      {@b@        throw new IllegalArgumentException("Cannot find Hibernate session factory implementation !");@b@      }@b@    }@b@    this._sessionFactory = ((SessionFactoryImpl)sessionFactory);@b@  }@b@@b@  public HibernateUtil()@b@  {@b@    this._session = new ThreadLocal();@b@    this._persistenceMap = new HashMap();@b@    this._unehancementMap = new HashMap();@b@@b@    this._persistenceMap.put(Byte.class, Boolean.valueOf(false));@b@    this._persistenceMap.put(Short.class, Boolean.valueOf(false));@b@    this._persistenceMap.put(Integer.class, Boolean.valueOf(false));@b@    this._persistenceMap.put(Long.class, Boolean.valueOf(false));@b@    this._persistenceMap.put(Float.class, Boolean.valueOf(false));@b@    this._persistenceMap.put(Double.class, Boolean.valueOf(false));@b@    this._persistenceMap.put(Boolean.class, Boolean.valueOf(false));@b@    this._persistenceMap.put(String.class, Boolean.valueOf(false));@b@  }@b@@b@  public Serializable getId(Object pojo)@b@  {@b@    return getId(pojo, getPersistentClass(pojo));@b@  }@b@@b@  public Serializable getId(Object pojo, Class<?> hibernateClass)@b@  {@b@    if (this._sessionFactory == null)@b@    {@b@      throw new NullPointerException("No Hibernate Session Factory defined !");@b@    }@b@@b@    if (!(isPersistentClass(hibernateClass)))@b@    {@b@      if (_log.isTraceEnabled())@b@      {@b@        _log.trace(hibernateClass + " is not persistent");@b@        dumpPersistenceMap();@b@      }@b@      throw new NotPersistentObjectException(pojo);@b@    }@b@@b@    ClassMetadata hibernateMetadata = this._sessionFactory.getClassMetadata(getEntityName(hibernateClass, pojo));@b@    if (hibernateMetadata == null)@b@    {@b@      throw new ComponentTypeException(pojo);@b@    }@b@@b@    Serializable id = null;@b@    Class pojoClass = getPersistentClass(pojo);@b@    if (hibernateClass.equals(pojoClass))@b@    {@b@      if (pojo instanceof HibernateProxy)@b@      {@b@        id = ((HibernateProxy)pojo).getHibernateLazyInitializer().getIdentifier();@b@      }@b@      else@b@      {@b@        id = hibernateMetadata.getIdentifier(pojo, EntityMode.POJO);@b@      }@b@@b@    }@b@    else@b@    {@b@      String property = hibernateMetadata.getIdentifierPropertyName();@b@      try@b@      {@b@        property = property.substring(0, 1).toUpperCase() + property.substring(1);@b@@b@        String getter = "get" + property;@b@@b@        Method method = pojoClass.getMethod(getter, (Class[])null);@b@        if (method == null)@b@        {@b@          throw new RuntimeException("Cannot find method " + getter + " for Class<?> " + pojoClass);@b@        }@b@        id = (Serializable)method.invoke(pojo, (Object[])null);@b@      }@b@      catch (Exception ex)@b@      {@b@        throw new RuntimeException("Invocation exception ", ex);@b@      }@b@@b@    }@b@@b@    if (isUnsavedValue(pojo, id, hibernateClass))@b@    {@b@      throw new TransientObjectException(pojo);@b@    }@b@    return id;@b@  }@b@@b@  public boolean isPersistentPojo(Object pojo)@b@  {@b@    if (pojo == null)@b@    {@b@      return false;@b@    }@b@@b@    try@b@    {@b@      getId(pojo);@b@      return true;@b@    }@b@    catch (TransientObjectException ex)@b@    {@b@      return false;@b@    }@b@    catch (NotPersistentObjectException ex) {@b@    }@b@    return false;@b@  }@b@@b@  public boolean isPersistentClass(Class<?> clazz)@b@  {@b@    if (this._sessionFactory == null)@b@    {@b@      throw new NullPointerException("No Hibernate Session Factory defined !");@b@    }@b@@b@    clazz = getUnenhancedClass(clazz);@b@@b@    synchronized (this._persistenceMap)@b@    {@b@      Boolean persistent = (Boolean)this._persistenceMap.get(clazz);@b@      if (persistent == null)@b@        break label55;@b@      label55: return persistent.booleanValue();@b@    }@b@@b@    computePersistenceForClass(clazz);@b@    return ((Boolean)this._persistenceMap.get(clazz)).booleanValue();@b@  }@b@@b@  public Class<?> getUnenhancedClass(Class<?> clazz)@b@  {@b@    Class unenhancedClass = (Class)this._unehancementMap.get(clazz);@b@    if (unenhancedClass == null)@b@    {@b@      unenhancedClass = UnEnhancer.unenhanceClass(clazz);@b@      this._unehancementMap.put(clazz, unenhancedClass);@b@    }@b@    return unenhancedClass;@b@  }@b@@b@  public boolean isEnhanced(Class<?> clazz)@b@  {@b@    return (clazz != getUnenhancedClass(clazz));@b@  }@b@@b@  public void openSession()@b@  {@b@    if (this._sessionFactory == null)@b@    {@b@      throw new NullPointerException("No Hibernate Session Factory defined !");@b@    }@b@@b@    Session session = this._sessionFactory.openSession();@b@@b@    this._session.set(session);@b@  }@b@@b@  public void closeCurrentSession()@b@  {@b@    Session session = (Session)this._session.get();@b@    if (session != null)@b@    {@b@      session.close();@b@      this._session.remove();@b@    }@b@  }@b@@b@  public Object load(Serializable id, Class<?> persistentClass)@b@  {@b@    persistentClass = getUnenhancedClass(persistentClass);@b@@b@    return getSession().get(persistentClass, id);@b@  }@b@@b@  public Map<String, Serializable> serializeEntityProxy(Object proxy)@b@  {@b@    if (proxy == null)@b@    {@b@      return null;@b@    }@b@@b@    Map result = new HashMap();@b@    result.put("class", getUnenhancedClass(proxy.getClass()).getName());@b@    result.put("id", getId(proxy));@b@@b@    return result;@b@  }@b@@b@  public Object createEntityProxy(Map<String, Serializable> proxyInformations)@b@  {@b@    Serializable id = (Serializable)proxyInformations.get("id");@b@    String entityName = (String)proxyInformations.get("class");@b@@b@    return getSession().load(entityName, id);@b@  }@b@@b@  public Map<String, Serializable> serializePersistentCollection(Object persistentCollection)@b@  {@b@    Map result = new HashMap();@b@@b@    AbstractPersistentCollection collection = (AbstractPersistentCollection)persistentCollection;@b@    result.put("class", collection.getClass().getName());@b@    result.put("role", collection.getRole());@b@    result.put("key", collection.getKey());@b@@b@    if (isInitialized(collection) == true)@b@    {@b@      if (collection instanceof Collection)@b@      {@b@        result.put("idList", createIdList((Collection)collection));@b@      }@b@      else if (collection instanceof Map)@b@      {@b@        Map map = (Map)collection;@b@        ArrayList keyList = createIdList(map.keySet());@b@        if (keyList != null)@b@        {@b@          result.put("idList", keyList);@b@@b@          ArrayList valueList = createIdList(map.values());@b@          if (keyList != null)@b@          {@b@            result.put("valueList", valueList);@b@          }@b@        }@b@      }@b@      else@b@      {@b@        throw new RuntimeException("Unexpected Persistent collection : " + collection.getClass());@b@      }@b@    }@b@@b@    return result;@b@  }@b@@b@  public Object createPersistentCollection(Map<String, Serializable> proxyInformations, Object underlyingCollection)@b@  {@b@    Iterator i$;@b@    Object key;@b@    Object addedItems = removeNewItems(proxyInformations, underlyingCollection);@b@    Collection deletedItems = addDeletedItems(proxyInformations, underlyingCollection);@b@@b@    String className = (String)proxyInformations.get("class");@b@@b@    Session session = getSession();@b@    PersistentCollection collection = null;@b@    if (PersistentBag.class.getName().equals(className))@b@    {@b@      if (underlyingCollection == null)@b@      {@b@        collection = new PersistentBag((SessionImpl)session);@b@      }@b@      else@b@      {@b@        collection = new PersistentBag((SessionImpl)session, (Collection)underlyingCollection);@b@      }@b@@b@    }@b@    else if (PersistentList.class.getName().equals(className))@b@    {@b@      if (underlyingCollection == null)@b@      {@b@        collection = new PersistentList((SessionImpl)session);@b@      }@b@      else@b@      {@b@        collection = new PersistentList((SessionImpl)session, (List)underlyingCollection);@b@      }@b@@b@    }@b@    else if (PersistentSet.class.getName().equals(className))@b@    {@b@      if (underlyingCollection == null)@b@      {@b@        collection = new PersistentSet((SessionImpl)session);@b@      }@b@      else@b@      {@b@        collection = new PersistentSet((SessionImpl)session, (Set)underlyingCollection);@b@      }@b@@b@    }@b@    else if (PersistentSortedSet.class.getName().equals(className))@b@    {@b@      if (underlyingCollection == null)@b@      {@b@        collection = new PersistentSortedSet((SessionImpl)session);@b@      }@b@      else@b@      {@b@        collection = new PersistentSortedSet((SessionImpl)session, (SortedSet)underlyingCollection);@b@      }@b@@b@    }@b@    else if (PersistentMap.class.getName().equals(className))@b@    {@b@      if (underlyingCollection == null)@b@      {@b@        collection = new PersistentMap((SessionImpl)session);@b@      }@b@      else@b@      {@b@        collection = new PersistentMap((SessionImpl)session, (Map)underlyingCollection);@b@      }@b@@b@    }@b@    else if (PersistentSortedMap.class.getName().equals(className))@b@    {@b@      if (underlyingCollection == null)@b@      {@b@        collection = new PersistentSortedMap((SessionImpl)session);@b@      }@b@      else@b@      {@b@        collection = new PersistentSortedMap((SessionImpl)session, (SortedMap)underlyingCollection);@b@      }@b@@b@    }@b@    else@b@    {@b@      throw new RuntimeException("Unknown persistent collection class name : " + className);@b@    }@b@@b@    String role = (String)proxyInformations.get("role");@b@    Serializable snapshot = null;@b@    if (underlyingCollection != null)@b@    {@b@      CollectionPersister collectionPersister = this._sessionFactory.getCollectionPersister(role);@b@      snapshot = collection.getSnapshot(collectionPersister);@b@    }@b@@b@    collection.setSnapshot((Serializable)proxyInformations.get("key"), role, snapshot);@b@@b@    if (deletedItems != null)@b@    {@b@      if (collection instanceof Collection)@b@      {@b@        for (i$ = deletedItems.iterator(); i$.hasNext(); ) { key = i$.next();@b@@b@          ((Collection)collection).remove(key);@b@        }@b@      }@b@      else if (collection instanceof Map)@b@      {@b@        for (i$ = deletedItems.iterator(); i$.hasNext(); ) { key = i$.next();@b@@b@          ((Map)collection).remove(key);@b@        }@b@@b@      }@b@@b@    }@b@@b@    if (addedItems != null)@b@    {@b@      List collectionList;@b@      if (collection instanceof List)@b@      {@b@        collectionList = (List)collection;@b@        for (NewItem key : (List)addedItems)@b@        {@b@          if (key.index < collectionList.size())@b@          {@b@            collectionList.add(key.index, key.object);@b@          }@b@          else@b@          {@b@            collectionList.add(key.object);@b@          }@b@        }@b@      }@b@      else if (collection instanceof Collection)@b@      {@b@        for (NewItem key : (List)addedItems)@b@        {@b@          ((Collection)collection).add(key.object);@b@        }@b@      }@b@      else if (collection instanceof Map)@b@      {@b@        ((Map)collection).putAll((Map)addedItems);@b@      }@b@    }@b@@b@    return collection;@b@  }@b@@b@  public boolean isPersistentCollection(Class<?> collectionClass)@b@  {@b@    return PersistentCollection.class.isAssignableFrom(collectionClass);@b@  }@b@@b@  public boolean isInitialized(Object proxy)@b@  {@b@    return Hibernate.isInitialized(proxy);@b@  }@b@@b@  public void initialize(Object proxy)@b@  {@b@    Hibernate.initialize(proxy);@b@  }@b@@b@  private void computePersistenceForClass(Class<?> clazz)@b@  {@b@    label23: Type[] types;@b@    int index;@b@    synchronized (this._persistenceMap)@b@    {@b@      if (this._persistenceMap.get(clazz) == null) {@b@        break label23;@b@      }@b@@b@      return;@b@    }@b@@b@    List entityNames = getEntityNamesFor(clazz);@b@    if ((entityNames == null) || (entityNames.isEmpty() == true))@b@    {@b@      int index;@b@      Class[] interfaces = clazz.getInterfaces();@b@      if (interfaces != null)@b@      {@b@        for (index = 0; index < interfaces.length; ++index)@b@        {@b@          if (isPersistentClass(interfaces[index]))@b@          {@b@            markClassAsPersistent(clazz, true);@b@            return;@b@          }@b@@b@        }@b@@b@      }@b@@b@      markClassAsPersistent(clazz, false);@b@      return;@b@    }@b@@b@    markClassAsPersistent(clazz, true);@b@@b@    for (String entityName : entityNames)@b@    {@b@      types = this._sessionFactory.getClassMetadata(entityName).getPropertyTypes();@b@      for (index = 0; index < types.length; ++index)@b@      {@b@        Type type = types[index];@b@        if (_log.isDebugEnabled())@b@        {@b@          _log.debug("Scanning type " + type.getName() + " from " + clazz);@b@        }@b@        computePersistentForType(type);@b@      }@b@    }@b@  }@b@@b@  private void markClassAsPersistent(Class<?> clazz, boolean persistent)@b@  {@b@    if (_log.isDebugEnabled())@b@    {@b@      if (persistent)@b@      {@b@        _log.debug("Marking " + clazz + " as persistent");@b@      }@b@      else@b@      {@b@        _log.debug("Marking " + clazz + " as not persistent");@b@      }@b@    }@b@    synchronized (this._persistenceMap)@b@    {@b@      if (this._persistenceMap.get(clazz) == null)@b@      {@b@        this._persistenceMap.put(clazz, Boolean.valueOf(persistent));@b@      }@b@      else if (persistent != ((Boolean)this._persistenceMap.get(clazz)).booleanValue())@b@      {@b@        throw new RuntimeException("Invalid persistence state for " + clazz);@b@      }@b@    }@b@  }@b@@b@  private void computePersistentForType(Type type)@b@  {@b@    label28: Type[] subtypes;@b@    int i;@b@    int i;@b@    synchronized (this._persistenceMap)@b@    {@b@      if (this._persistenceMap.get(type.getReturnedClass()) == null) {@b@        break label28;@b@      }@b@@b@      return;@b@    }@b@@b@    if (_log.isDebugEnabled())@b@    {@b@      _log.debug("Scanning type " + type.getName());@b@    }@b@@b@    if (type.isComponentType())@b@    {@b@      if (_log.isDebugEnabled())@b@      {@b@        _log.debug("Type " + type.getName() + " is component type");@b@      }@b@@b@      markClassAsPersistent(type.getReturnedClass(), true);@b@@b@      subtypes = ((AbstractComponentType)type).getSubtypes();@b@      for (i = 0; i < subtypes.length; ++i)@b@      {@b@        computePersistentForType(subtypes[i]);@b@      }@b@    }@b@    else if (IUserType.class.isAssignableFrom(type.getReturnedClass()))@b@    {@b@      if (_log.isDebugEnabled())@b@      {@b@        _log.debug("Type " + type.getName() + " is user type");@b@      }@b@@b@      markClassAsPersistent(type.getReturnedClass(), true);@b@    }@b@    else if (type.isCollectionType())@b@    {@b@      if (_log.isDebugEnabled())@b@      {@b@        _log.debug("Type " + type.getName() + " is collection type");@b@      }@b@      computePersistentForType(((CollectionType)type).getElementType(this._sessionFactory));@b@    }@b@    else if (type.isEntityType())@b@    {@b@      if (_log.isDebugEnabled())@b@      {@b@        _log.debug("Type " + type.getName() + " is entity type");@b@      }@b@      computePersistenceForClass(type.getReturnedClass());@b@    }@b@  }@b@@b@  private void dumpPersistenceMap()@b@  {@b@    synchronized (this._persistenceMap)@b@    {@b@      _log.trace("-- Start of persistence map --");@b@      for (Map.Entry persistenceEntry : this._persistenceMap.entrySet())@b@      {@b@        _log.trace(persistenceEntry.getKey() + " persistence is " + persistenceEntry.getValue());@b@      }@b@      _log.trace("-- End of persistence map --");@b@    }@b@  }@b@@b@  private ArrayList<SerializableId> createIdList(Collection collection)@b@  {@b@    if (collection == null)@b@    {@b@      return null;@b@    }@b@@b@    int size = collection.size();@b@    ArrayList idList = new ArrayList(size);@b@@b@    Iterator iterator = collection.iterator();@b@    while (iterator.hasNext())@b@    {@b@      Object item = iterator.next();@b@@b@      SerializableId id = new SerializableId();@b@@b@      if (isPersistentPojo(item))@b@      {@b@        id.setEntityName(getEntityName(item.getClass(), item));@b@        id.setId(getId(item));@b@      }@b@      else@b@      {@b@        id.setEntityName(item.getClass().getName());@b@        id.setHashCode(Integer.valueOf(item.hashCode()));@b@      }@b@@b@      idList.add(id);@b@    }@b@@b@    if (idList.isEmpty())@b@    {@b@      return null;@b@    }@b@@b@    return idList;@b@  }@b@@b@  private List<NewItem> getDeletedItemsForCollection(Collection collection, ArrayList<SerializableId> idList)@b@  {@b@    ArrayList collectionID = createIdList(collection);@b@@b@    ArrayList deletedItems = new ArrayList();@b@    for (SerializableId sid : idList)@b@    {@b@      if ((collectionID == null) || (!(collectionID.contains(sid))))@b@      {@b@        NewItem deleted = new NewItem();@b@@b@        if (_log.isDebugEnabled())@b@        {@b@          _log.debug("Deleted item " + sid.getEntityName() + "[" + sid.getId() + "]");@b@        }@b@        try@b@        {@b@          if (sid.getId() != null)@b@          {@b@            deleted.object = getSession().load(sid.getEntityName(), sid.getId());@b@            deleted.index = idList.indexOf(sid);@b@            deletedItems.add(deleted);@b@          }@b@@b@        }@b@        catch (Exception e)@b@        {@b@          throw new RuntimeException(e);@b@        }@b@      }@b@    }@b@@b@    if (deletedItems.isEmpty())@b@    {@b@      return null;@b@    }@b@@b@    if (_log.isDebugEnabled())@b@    {@b@      _log.debug("Found " + deletedItems.size() + " deleted item(s) ");@b@    }@b@    return deletedItems;@b@  }@b@@b@  private List<NewItem> getNewItemsForCollection(Collection collection, ArrayList<SerializableId> idList)@b@  {@b@    List addedItems = new ArrayList();@b@    Iterator iterator = collection.iterator();@b@    while (iterator.hasNext())@b@    {@b@      int hashCode;@b@      boolean found;@b@      Iterator i$;@b@      SerializableId sid;@b@      Object currentItem = iterator.next();@b@      try@b@      {@b@        Serializable id = getId(currentItem);@b@@b@        boolean found = false;@b@        for (SerializableId sid : idList)@b@        {@b@          if (sid.getId().equals(id))@b@          {@b@            found = true;@b@            break;@b@          }@b@        }@b@@b@        if (!(found))@b@        {@b@          addedItems.add(createNewItem(currentItem, collection));@b@        }@b@@b@      }@b@      catch (TransientObjectException ex)@b@      {@b@        hashCode = currentItem.hashCode();@b@@b@        found = false;@b@        for (i$ = idList.iterator(); i$.hasNext(); ) { sid = (SerializableId)i$.next();@b@@b@          if ((sid.getHashCode() != null) && (sid.getHashCode().intValue() == hashCode))@b@          {@b@            found = true;@b@            break;@b@          }@b@        }@b@@b@        if (!(found))@b@        {@b@          addedItems.add(createNewItem(currentItem, collection));@b@        }@b@@b@      }@b@      catch (NotPersistentObjectException ex2)@b@      {@b@        hashCode = currentItem.hashCode();@b@@b@        found = false;@b@        for (i$ = idList.iterator(); i$.hasNext(); ) { sid = (SerializableId)i$.next();@b@@b@          if ((sid.getHashCode() != null) && (sid.getHashCode().intValue() == hashCode))@b@          {@b@            found = true;@b@            break;@b@          }@b@        }@b@@b@        if (!(found))@b@        {@b@          addedItems.add(createNewItem(currentItem, collection));@b@        }@b@      }@b@    }@b@@b@    if (addedItems.isEmpty())@b@    {@b@      return null;@b@    }@b@@b@    if (_log.isDebugEnabled())@b@    {@b@      _log.debug("Found " + addedItems.size() + " new item(s)");@b@    }@b@    return addedItems;@b@  }@b@@b@  private NewItem createNewItem(Object currentItem, Collection<?> mergedCollection)@b@  {@b@    if (_log.isDebugEnabled())@b@    {@b@      _log.debug("New item " + currentItem);@b@    }@b@    NewItem newItem = new NewItem();@b@    newItem.object = currentItem;@b@@b@    if (mergedCollection instanceof List)@b@    {@b@      newItem.index = ((List)mergedCollection).indexOf(currentItem);@b@    }@b@    return newItem;@b@  }@b@@b@  private Collection<?> addDeletedItems(Map<String, Serializable> proxyInformations, Object underlyingCollection)@b@  {@b@    ArrayList idList;@b@    if (underlyingCollection instanceof Collection)@b@    {@b@      Collection collection = (Collection)underlyingCollection;@b@      idList = (ArrayList)proxyInformations.get("idList");@b@      if (idList != null)@b@      {@b@        Iterator i$;@b@        NewItem deletedItem;@b@        List deletedItemList = getDeletedItemsForCollection(collection, idList);@b@        ArrayList deletedList = null;@b@@b@        if (deletedItemList != null)@b@        {@b@          deletedList = new ArrayList(deletedItemList.size());@b@@b@          if (collection instanceof List)@b@          {@b@            for (i$ = deletedItemList.iterator(); i$.hasNext(); ) { deletedItem = (NewItem)i$.next();@b@@b@              ((List)collection).add(deletedItem.index, deletedItem.object);@b@              deletedList.add(deletedItem.object);@b@            }@b@@b@          }@b@          else@b@            for (i$ = deletedItemList.iterator(); i$.hasNext(); ) { deletedItem = (NewItem)i$.next();@b@@b@              collection.add(deletedItem.object);@b@              deletedList.add(deletedItem.object);@b@            }@b@@b@        }@b@@b@        return deletedList;@b@      }@b@    }@b@    else if (underlyingCollection instanceof Map)@b@    {@b@      Map map = (Map)underlyingCollection;@b@      idList = (ArrayList)proxyInformations.get("idList");@b@      if (idList != null)@b@      {@b@        List deletedValueList;@b@        int index;@b@        List deletedKeyList = getDeletedItemsForCollection(map.keySet(), idList);@b@@b@        if (deletedKeyList != null)@b@        {@b@          deletedValueList = null;@b@          ArrayList valueList = (ArrayList)proxyInformations.get("valueList");@b@          if (valueList != null)@b@          {@b@            deletedValueList = getDeletedItemsForCollection(map.values(), idList);@b@          }@b@@b@          int deleteCount = deletedKeyList.size();@b@          for (index = 0; index < deleteCount; ++index)@b@          {@b@            NewItem key = (NewItem)deletedKeyList.get(index);@b@            NewItem value = null;@b@            if ((deletedValueList != null) && (index < deletedValueList.size()))@b@            {@b@              value = (NewItem)deletedValueList.get(index);@b@            }@b@            map.put(key.object, value.object);@b@          }@b@        }@b@        return deletedKeyList;@b@      }@b@    }@b@@b@    return null;@b@  }@b@@b@  private Object removeNewItems(Map<String, Serializable> proxyInformations, Object underlyingCollection)@b@  {@b@    ArrayList idList;@b@    if (underlyingCollection instanceof Collection)@b@    {@b@      Collection collection = (Collection)underlyingCollection;@b@      idList = (ArrayList)proxyInformations.get("idList");@b@      if (idList == null)@b@      {@b@        idList = new ArrayList();@b@      }@b@      List newItemList = getNewItemsForCollection(collection, idList);@b@@b@      if (newItemList != null)@b@      {@b@        for (NewItem item : newItemList)@b@        {@b@          collection.remove(item.object);@b@        }@b@      }@b@@b@      return newItemList;@b@    }@b@    if (underlyingCollection instanceof Map)@b@    {@b@      Map map = (Map)underlyingCollection;@b@      idList = (ArrayList)proxyInformations.get("idList");@b@      if (idList == null)@b@      {@b@        idList = new ArrayList();@b@      }@b@@b@      List newKeyList = getNewItemsForCollection(map.keySet(), idList);@b@      Map newItemMap = new HashMap();@b@      if (newKeyList != null)@b@      {@b@        for (NewItem key : newKeyList)@b@        {@b@          newItemMap.put(key.object, map.get(key.object));@b@          map.remove(key.object);@b@        }@b@      }@b@      return newItemMap;@b@    }@b@@b@    return null;@b@  }@b@@b@  private boolean isUnsavedValue(Object pojo, Serializable id, Class<?> persistentClass)@b@  {@b@    if (id == null)@b@    {@b@      return true;@b@    }@b@@b@    EntityPersister entityPersister = this._sessionFactory.getEntityPersister(getEntityName(persistentClass, pojo));@b@    EntityMetamodel metamodel = entityPersister.getEntityMetamodel();@b@    IdentifierProperty idProperty = metamodel.getIdentifierProperty();@b@    Boolean result = idProperty.getUnsavedValue().isUnsaved(id);@b@@b@    if (result == null)@b@    {@b@      return false;@b@    }@b@@b@    return result.booleanValue();@b@  }@b@@b@  private Class<?> getPersistentClass(Object pojo)@b@  {@b@    if (pojo instanceof HibernateProxy)@b@    {@b@      return ((HibernateProxy)pojo).getHibernateLazyInitializer().getPersistentClass();@b@    }@b@@b@    return pojo.getClass();@b@  }@b@@b@  private Session getSession()@b@  {@b@    Session session = (Session)this._session.get();@b@    if (session == null)@b@    {@b@      openSession();@b@      session = (Session)this._session.get();@b@    }@b@    return session;@b@  }@b@@b@  private String getEntityName(Class<?> clazz, Object pojo)@b@  {@b@    ClassMetadata metadata = this._sessionFactory.getClassMetadata(clazz);@b@    if (metadata != null)@b@    {@b@      return metadata.getEntityName();@b@    }@b@@b@    List entityNames = getEntityNamesFor(clazz);@b@@b@    if (entityNames.isEmpty())@b@    {@b@      return null;@b@    }@b@    if (entityNames.size() == 1)@b@    {@b@      return ((String)entityNames.get(0));@b@    }@b@@b@    if (pojo != null)@b@    {@b@      return ((SessionImpl)getSession()).bestGuessEntityName(pojo);@b@    }@b@@b@    throw new NullPointerException("Missing pojo for entity name retrieving !");@b@  }@b@@b@  private List<String> getEntityNamesFor(Class<?> clazz)@b@  {@b@    List entityNames = new ArrayList();@b@    Map allMetadata = this._sessionFactory.getAllClassMetadata();@b@    for (ClassMetadata classMetadata : allMetadata.values())@b@    {@b@      if (clazz.equals(classMetadata.getMappedClass(EntityMode.POJO)))@b@      {@b@        entityNames.add(classMetadata.getEntityName());@b@      }@b@    }@b@@b@    return entityNames;@b@  }@b@}

2.NewItem

package net.sf.gilead.core.hibernate;@b@@b@class NewItem@b@{@b@  public Object object;@b@  public int index;@b@}