protected final Collection<?> getCollection(Class<?> type,Collection<?> value) { if (value instanceof PersistentSet && type.isAssignableFrom(HashSet.class)) { return new HashSet<>(); }else if(value instanceof PersistentList && type.isAssignableFrom(ArrayList.class)){ return new ArrayList<>(); } return value; }
@SuppressWarnings({ "unchecked", "rawtypes" }) private Collection<?> deepLoadCollection(Collection collection, Collection guideObj) { Collection result = null; if(guideObj != null && !guideObj.isEmpty() && collection != null && !collection.isEmpty()){ try { if (collection instanceof PersistentSet) { result = new LinkedHashSet<>(); }else if (collection instanceof PersistentList){ result = new ArrayList<>(); } else { result = collection.getClass().newInstance(); } //Recuperar primera instancia del guideObj y usarlo como siguiente guideObj Object collGuideObj = guideObj.iterator().next(); for (Object aux : collection) { result.add(deepLoad(aux, collGuideObj)); } collection.clear(); collection.addAll(result); } catch (Throwable e) { e.printStackTrace(); } } return collection; }
/** * Hibernate related cloning. * <p/> * {@inheritDoc} */ @SuppressWarnings("unchecked") @Override protected <E> E cloneUninitializedProperty(Object owner, E propertyValue) { E clonedPropertyValue = propertyValue; if (isInitialized(owner)) { if (propertyValue instanceof PersistentCollection) { if (unwrapProxy((((PersistentCollection) propertyValue).getOwner())) != unwrapProxy(owner)) { if (propertyValue instanceof PersistentSet) { clonedPropertyValue = (E) new PersistentSet( // Must reset the session. // See bug #902 /* ((PersistentSet) propertyValue).getSession() */null); } else if (propertyValue instanceof PersistentList) { clonedPropertyValue = (E) new PersistentList( // Must reset the session. // See bug #902 /* ((PersistentList) propertyValue).getSession() */null); } changeCollectionOwner((Collection<?>) clonedPropertyValue, owner); ((PersistentCollection) clonedPropertyValue).setSnapshot(((PersistentCollection) propertyValue).getKey(), ((PersistentCollection) propertyValue).getRole(), null); } } else { if (propertyValue instanceof HibernateProxy) { return (E) getHibernateSession().load( ((HibernateProxy) propertyValue).getHibernateLazyInitializer().getEntityName(), ((IEntity) propertyValue).getId()); } } } return clonedPropertyValue; }
@Override public void postBootHook(AbstractApplicationContext ctx) { if(ctx.containsBean("xstream")){ XStream xStream = (XStream)ctx.getBean("xstream"); xStream.registerConverter(new HibernateProxyConverter()); xStream.registerConverter(new HibernatePersistentCollectionConverter(xStream.getMapper())); xStream.alias("list", PersistentList.class); xStream.alias("list", PersistentBag.class); xStream.alias("set", PersistentSet.class); } }
public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister, Serializable key) { return new PersistentList(session); }
public PersistentCollection wrap(SessionImplementor session, Object collection) { return new PersistentList( session, (List) collection ); }
public boolean canConvert(final Class type) { return type == PersistentBag.class || type == PersistentList.class || type == PersistentSet.class; }