/** * Show collection loading */ @Test public void testShowCollectionLoading() { setTestUserContext(); final OBQuery<Order> orderQuery = OBDal.getInstance().createQuery(Order.class, ""); orderQuery.setMaxResult(1); Order order = orderQuery.uniqueResult(); final List<OrderLine> orderLineList = order.getOrderLineList(); // is a hibernate special thing Assert.assertTrue(orderLineList instanceof PersistentBag); // this will load the list, all OrderLines are loaded in Memory System.err.println(orderLineList.size()); }
public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister, Serializable key) throws HibernateException { if ( session.getEntityMode()==EntityMode.DOM4J ) { return new PersistentElementHolder(session, persister, key); } else { return new PersistentBag(session); } }
public PersistentCollection wrap(SessionImplementor session, Object collection) { if ( session.getEntityMode()==EntityMode.DOM4J ) { return new PersistentElementHolder( session, (Element) collection ); } else { return new PersistentBag( session, (Collection) collection ); } }
public void testWriteMethodDirtying() { BagOwner parent = new BagOwner( "root" ); BagOwner child = new BagOwner( "c1" ); parent.getChildren().add( child ); child.setParent( parent ); BagOwner otherChild = new BagOwner( "c2" ); Session session = openSession(); session.beginTransaction(); session.save( parent ); session.flush(); // at this point, the list on parent has now been replaced with a PersistentBag... PersistentBag children = ( PersistentBag ) parent.getChildren(); assertFalse( children.remove( otherChild ) ); assertFalse( children.isDirty() ); ArrayList otherCollection = new ArrayList(); otherCollection.add( child ); assertFalse( children.retainAll( otherCollection ) ); assertFalse( children.isDirty() ); otherCollection = new ArrayList(); otherCollection.add( otherChild ); assertFalse( children.removeAll( otherCollection ) ); assertFalse( children.isDirty() ); children.clear(); session.delete( child ); assertTrue( children.isDirty() ); session.flush(); children.clear(); assertFalse( children.isDirty() ); session.delete( parent ); session.getTransaction().commit(); session.close(); }
public void init() { collectionMap.put(PersistentBag.class, ArrayList.class); collectionMap.put(PersistentList.class, ArrayList.class); collectionMap.put(PersistentMap.class, HashMap.class); collectionMap.put(PersistentSet.class, HashSet.class); collectionMap.put(PersistentSortedMap.class, TreeMap.class); collectionMap.put(PersistentSortedSet.class, TreeSet.class); }
public static void setObjectPropertyDeep(Object bo, String propertyName, Class type, Object propertyValue, int depth) throws FormatException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { // Base return cases to avoid null pointers & infinite loops if (depth == 0 || isNull(bo) || !PropertyUtils.isReadable(bo, propertyName)) { return; } // need to materialize the updateable collections before resetting the property, because it may be used in the retrieval try { materializeUpdateableCollections(bo); } catch(ClassNotPersistableException ex){ //Not all classes will be persistable in a collection. For e.g. externalizable business objects. LOG.info("Not persistable dataObjectClass: "+bo.getClass().getName()+", field: "+propertyName); } // Set the property in the BO setObjectProperty(bo, propertyName, type, propertyValue); // Now drill down and check nested BOs and BO lists PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(bo.getClass()); for (int i = 0; i < propertyDescriptors.length; i++) { PropertyDescriptor propertyDescriptor = propertyDescriptors[i]; // Business Objects if (propertyDescriptor.getPropertyType() != null && (BusinessObject.class).isAssignableFrom( propertyDescriptor.getPropertyType()) && PropertyUtils.isReadable(bo, propertyDescriptor.getName())) { Object nestedBo = getPropertyValue(bo, propertyDescriptor.getName()); if (nestedBo instanceof BusinessObject) { setObjectPropertyDeep((BusinessObject) nestedBo, propertyName, type, propertyValue, depth - 1); } } // Lists else if (propertyDescriptor.getPropertyType() != null && (List.class).isAssignableFrom( propertyDescriptor.getPropertyType()) && getPropertyValue(bo, propertyDescriptor.getName()) != null) { List propertyList = (List) getPropertyValue(bo, propertyDescriptor.getName()); // Complete Hibernate Hack - fetches the proxied List into the PersistenceContext and sets it on the BO Copy. if (propertyList instanceof PersistentBag) { try { PersistentBag bag = (PersistentBag) propertyList; PersistableBusinessObject pbo = (PersistableBusinessObject) KRADServiceLocator.getEntityManagerFactory() .createEntityManager().find(bo.getClass(), bag.getKey()); Field field1 = pbo.getClass().getDeclaredField(propertyDescriptor.getName()); Field field2 = bo.getClass().getDeclaredField(propertyDescriptor.getName()); field1.setAccessible(true); field2.setAccessible(true); field2.set(bo, field1.get(pbo)); propertyList = (List) getPropertyValue(bo, propertyDescriptor.getName()); ; } catch (Exception e) { LOG.error(e.getMessage(), e); } } // End Complete Hibernate Hack for (Object listedBo : propertyList) { if (listedBo != null && listedBo instanceof BusinessObject) { setObjectPropertyDeep(listedBo, propertyName, type, propertyValue, depth - 1); } } // end for } } // end for }