@Override public <S, D> boolean shouldMap(Type<S> type, String s, S s1, Type<D> type1, String s2, D d, MappingContext mappingContext) { if (type != null && s1 != null) { if (type.isCollection()) { if (s1 instanceof AbstractPersistentCollection) { return false; } if (((PersistentSet) s1).wasInitialized()) { return false; } } } return true; }
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; }
/** * If object is proxy, get unwrapped non-proxy object. * * @param proxy Object to check and unwrap * @return Unwrapped object if proxyied, if not just returns same object */ @SuppressWarnings( "unchecked" ) public static <T> T unwrap( T proxy ) { if ( !isProxy( proxy ) ) { return proxy; } Hibernate.initialize( proxy ); if ( HibernateProxy.class.isInstance( proxy ) ) { Object result = ((HibernateProxy) proxy).writeReplace(); if ( !SerializableProxy.class.isInstance( result ) ) { return (T) result; } } if ( PersistentCollection.class.isInstance( proxy ) ) { PersistentCollection persistentCollection = (PersistentCollection) proxy; if ( PersistentSet.class.isInstance( persistentCollection ) ) { Map<?, ?> map = (Map<?, ?>) persistentCollection.getStoredSnapshot(); return (T) new LinkedHashSet<>( map.keySet() ); } return (T) persistentCollection.getStoredSnapshot(); } return proxy; }
/** * Tests fix for bug #928. */ @Test @SuppressWarnings("unchecked") public void testJoinOrderBy() { final HibernateBackendController hbc = (HibernateBackendController) getBackendController(); EnhancedDetachedCriteria crit = EnhancedDetachedCriteria.forClass(Department.class); DetachedCriteria companyCrit = crit.getSubCriteriaFor(crit, Department.COMPANY, JoinType.INNER_JOIN); companyCrit.add(Restrictions.eq(Nameable.NAME, "Design2See")); DetachedCriteria teamsCrit = crit.getSubCriteriaFor(crit, Department.TEAMS, JoinType.LEFT_OUTER_JOIN); teamsCrit.add(Restrictions.eq(OrganizationalUnit.OU_ID, "HR-001")); crit.addOrder(Order.desc(Nameable.NAME)); crit.addOrder(Order.asc(IEntity.ID)); List<Department> depts = hbc.findByCriteria(crit, null, Department.class); for (Department d : depts) { // force collection sorting. Set<Team> teams = d.getTeams(); Set<?> innerSet; try { if (teams instanceof ICollectionWrapper<?>) { teams = (Set<Team>) ((ICollectionWrapper) teams).getWrappedCollection(); } innerSet = (Set<?>) ReflectHelper.getPrivateFieldValue(PersistentSet.class, "set", teams); } catch (Exception ex) { throw new RuntimeException(ex); } assertTrue("innerSet is a LinkedHashSet", LinkedHashSet.class.isInstance(innerSet)); } }
@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; }
/** * Ensures that the collection held by a Persistent Set is actually a * LinkedHashSet. * * @param collection * the collection to ensure implementation of. */ public static void ensureInnerLinkedHashSet(Collection<?> collection) { if (collection instanceof PersistentSet) { try { Set<?> innerSet = (Set<?>) ReflectHelper.getPrivateFieldValue( PersistentSet.class, "set", collection); if (innerSet != null && !(innerSet instanceof LinkedHashSet<?>)) { ReflectHelper.setPrivateFieldValue(PersistentSet.class, "set", collection, new LinkedHashSet<>(innerSet)); } } catch (Exception ex) { LOG.error("Failed to replace internal Hibernate set implementation"); } } }
/** * 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 PersistentSet(session); }
public PersistentCollection wrap(SessionImplementor session, Object collection) { return new PersistentSet( session, (java.util.Set) collection ); }
public boolean canConvert(final Class type) { return type == PersistentBag.class || type == PersistentList.class || type == PersistentSet.class; }