List of usage examples for org.hibernate.collection.spi PersistentCollection wasInitialized
boolean wasInitialized();
From source file:ch.algotrader.dao.GenericDaoImpl.java
License:Open Source License
@Override public Object getInitializedCollection(final String role, final long id) { return this.txTemplate.execute(txStatus -> { SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) this.sessionFactory; CollectionPersister persister = sessionFactoryImpl.getCollectionPersister(role); // load the owner entity ClassMetadata ownerMetadata = persister.getOwnerEntityPersister().getClassMetadata(); Session session = this.sessionFactory.getCurrentSession(); Object owner = session.get(ownerMetadata.getEntityName(), id); // owner does not exist anymore so no point in loading the collection if (owner == null) { return null; }/* w w w . j a v a2 s. c om*/ // get the collection by it's property name Object col = ownerMetadata.getPropertyValue(owner, persister.getNodeName()); // if it is a PersistentCollection make sure it is initialized if (col instanceof PersistentCollection) { PersistentCollection collection = (PersistentCollection) col; if (!collection.wasInitialized()) { collection.forceInitialization(); } } return col; }); }
From source file:com.sdl.odata.datasource.jpa.ODataProxyProcessor.java
License:Open Source License
private void unproxyElements(Object entity, Set<Object> visitedEntities, List<String> expandProperties) throws ODataDataSourceException { if (visitedEntities.contains(entity)) { return;// w w w . j a v a 2s . c o m } //put entity to set of already visited visitedEntities.add(entity); Class reflectObj = entity.getClass(); Field[] fields = reflectObj.getDeclaredFields(); boolean sourceEntity = visitedEntities.size() == 1; for (Field field : fields) { field.setAccessible(true); try { Object fieldType = field.get(entity); if (isJPAEntity(fieldType)) { unproxyElements(fieldType, visitedEntities, expandProperties); } else if (fieldType instanceof PersistentCollection) { PersistentCollection collection = (PersistentCollection) fieldType; if (collection.wasInitialized()) { for (Object element : (Iterable<?>) collection) { unproxyElements(element, visitedEntities, expandProperties); } } else { if (!sourceEntity && !isEntityExpanded(entity, expandProperties)) { if (fieldType instanceof List) { // Hibernate checks if the current field type is a hibernate proxy LOG.debug("This collection is lazy initialized. Replaced by an empty list."); field.set(entity, new ArrayList<>()); } else if (fieldType instanceof Set) { LOG.debug("This set is lazy initialized. Replaced by an empty set."); field.set(entity, new HashSet<>()); } } else { // These properties will be loaded during mapping field.set(entity, null); } } } } catch (IllegalAccessException e) { throw new ODataDataSourceException("Cannot un-proxy elements of: " + reflectObj.getName(), e); } } }
From source file:org.babyfish.hibernate.internal.SessionImplWrapper.java
License:Open Source License
@SuppressWarnings("unchecked") protected static void delete(XSessionImplementor sessionProxy, Object object) { if (object != null) { SessionFactory sessionFactory = sessionProxy.getRawSessionImpl().getSessionFactory(); PersistenceContext persistenceContext = ((org.hibernate.internal.SessionImpl) sessionProxy .getRawSessionImpl()).getPersistenceContext(); Map<PersistentCollection, CollectionEntry> collectionEntries = persistenceContext .getCollectionEntries(); for (Entry<PersistentCollection, CollectionEntry> entry : collectionEntries.entrySet()) { PersistentCollection persistentCollection = entry.getKey(); if (persistentCollection.wasInitialized()) { CollectionMetadata collectionMetadata = sessionFactory .getCollectionMetadata(persistentCollection.getRole()); Class<?> elementClass = collectionMetadata.getElementType().getReturnedClass(); if (elementClass.isAssignableFrom(object.getClass())) { if (persistentCollection instanceof Map<?, ?>) { ((Map<?, ?>) persistentCollection).values().remove(object); } else if (persistentCollection instanceof Collection<?>) { ((Collection<?>) persistentCollection).remove(object); }/*from w w w . j av a2s . c o m*/ } } } Class<?> clazz = object.getClass(); Collection<HbmReference> hbmReferences = null; NavigableMap<Class<?>, Collection<HbmReference>> hbmReferencesByTargetClass = (NavigableMap<Class<?>, Collection<HbmReference>>) ((XSessionFactoryImplementor) sessionProxy .getFactory()).getInternalData(SessionFactoryImplWrapper.IDK_HBM_REFERENCES_BY_TARGET_CLASS); for (Entry<Class<?>, Collection<HbmReference>> entry : hbmReferencesByTargetClass.descendingMap() .entrySet()) { if (entry.getKey().isAssignableFrom(clazz)) { hbmReferences = entry.getValue(); break; } } if (hbmReferences != null) { EntityReferenceComparator<? super Object> referenceComparator = EntityReferenceComparator .getInstance(); Entry<Object, EntityEntry>[] entityEntries = persistenceContext.reentrantSafeEntityEntries(); if (entityEntries != null) { for (Entry<Object, EntityEntry> entry : entityEntries) { Object entity = entry.getKey(); if (Hibernate.isInitialized(entity)) { EntityPersister persister = entry.getValue().getPersister(); ClassMetadata classMetadata = persister.getClassMetadata(); for (HbmReference hbmReference : hbmReferences) { if (hbmReference.ownerMetadata == classMetadata) { Object expectedObject = persister.getPropertyValue(entity, hbmReference.propertyIndex); if (referenceComparator.same(expectedObject, object)) { persister.setPropertyValue(entity, hbmReference.propertyIndex, null); } } } } } } } } sessionProxy.getRawSessionImpl().delete(object); }
From source file:org.grails.orm.hibernate.proxy.HibernateProxyHandler.java
License:Apache License
public void initialize(Object o) { if (o instanceof PersistentCollection) { final PersistentCollection col = (PersistentCollection) o; if (!col.wasInitialized()) { col.forceInitialization();/*w ww.j a v a 2 s .c om*/ } } super.initialize(o); }
From source file:org.granite.hibernate4.jmf.AbstractPersistentCollectionCodec.java
License:Open Source License
public void encode(ExtendedObjectOutput out, Object v) throws IOException, IllegalAccessException { JMFPersistentCollectionSnapshot snapshot = null; PersistentCollection collection = (PersistentCollection) v; if (!collection.wasInitialized()) snapshot = new JMFPersistentCollectionSnapshot( collection instanceof SortedSet || collection instanceof SortedMap, null); else if (collection instanceof Map) snapshot = new JMFPersistentCollectionSnapshot(true, null, collection.isDirty(), (Map<?, ?>) collection); else/*ww w. j av a 2 s . c o m*/ snapshot = new JMFPersistentCollectionSnapshot(true, null, collection.isDirty(), (Collection<?>) collection); snapshot.writeExternal(out); }
From source file:org.granite.hibernate4.jmf.AbstractPersistentCollectionCodec.java
License:Open Source License
@SuppressWarnings("unchecked") public void decode(ExtendedObjectInput in, Object v) throws IOException, ClassNotFoundException, IllegalAccessException { PersistentCollection collection = (PersistentCollection) v; if (collection.wasInitialized()) { boolean sorted = (collection instanceof SortedSet || collection instanceof SortedMap); PersistentCollectionSnapshot snapshot = new JMFPersistentCollectionSnapshot(sorted, null); snapshot.readCoreData(in);/*from ww w .j a v a 2 s.c o m*/ if (collection instanceof Map) ((Map<Object, Object>) collection).putAll(snapshot.getElementsAsMap()); else ((Collection<Object>) collection).addAll(snapshot.getElementsAsCollection()); if (snapshot.isDirty()) collection.dirty(); else collection.clearDirty(); } }
From source file:org.nextframework.persistence.HibernateUtils.java
License:Open Source License
@SuppressWarnings("unchecked") public static <E> E getLazyValue(E value) { if (value instanceof HibernateProxy) { LazyInitializer hibernateLazyInitializer = ((HibernateProxy) value).getHibernateLazyInitializer(); if (hibernateLazyInitializer.isUninitialized()) { try { Class<?> superclass = value.getClass().getSuperclass(); Serializable identifier = hibernateLazyInitializer.getIdentifier(); value = loadValue(value, superclass, identifier); } catch (IllegalArgumentException e) { } catch (SecurityException e) { }//from ww w . j a v a 2 s . c o m } } else if (value instanceof PersistentCollection) { try { PersistentCollection collection = (PersistentCollection) value; if (!collection.wasInitialized()) { Object owner = collection.getOwner(); String role = collection.getRole(); value = (E) DAOUtils.getDAOForClass(owner.getClass()).loadCollection(owner, role.substring(role.lastIndexOf('.') + 1)); System.out.println("COLECAO LAZY " + owner.getClass().getSimpleName() + "." + role); } } catch (Exception e) { //se nao conseguir carregar o valor lazy, no fazer nada } } return value; }