Package org.apache.ojb.broker.metadata

Examples of org.apache.ojb.broker.metadata.ObjectReferenceDescriptor


            {
                referencesBroker.retrieveCollection(pInstance, cld, cod, true);
            }
            else
            {
                ObjectReferenceDescriptor ord = cld.getObjectReferenceDescriptorByName(pAttributeName);
                if (ord != null)
                {
                    referencesBroker.retrieveReference(pInstance, cld, ord, true);
                }
                else
View Full Code Here


     */
    public void checkRefreshRelationships(Object obj, Identity oid, ClassDescriptor cld)
    {
        Iterator iter;
        CollectionDescriptor cds;
        ObjectReferenceDescriptor rds;
        // to avoid problems with circular references, locally cache the current object instance
        Object tmp = getInternalCache().doLocalLookup(oid);
        if(tmp != null && getInternalCache().isEnabledMaterialisationCache())
        {
            /*
            arminw: This should fix OJB-29, infinite loops on bidirectional 1:1 relations with
            refresh attribute 'true' for both references. OJB now assume that the object is already
            refreshed when it's cached in the materialisation cache
            */
            return;
        }
        try
        {
            getInternalCache().enableMaterializationCache();
            if(tmp == null)
            {
                getInternalCache().doInternalCache(oid, obj, MaterializationCache.TYPE_TEMP);
            }
            if(logger.isDebugEnabled()) logger.debug("Refresh relationships for " + oid);
            iter = cld.getCollectionDescriptors().iterator();
            while (iter.hasNext())
            {
                cds = (CollectionDescriptor) iter.next();
                if (cds.isRefresh())
                {
                    referencesBroker.retrieveCollection(obj, cld, cds, false);
                }
            }
            iter = cld.getObjectReferenceDescriptors().iterator();
            while (iter.hasNext())
            {
                rds = (ObjectReferenceDescriptor) iter.next();
                if (rds.isRefresh())
                {
                    referencesBroker.retrieveReference(obj, cld, rds, false);
                }
            }
            getInternalCache().disableMaterializationCache();
View Full Code Here

    }

    public void testAutoRefreshTrue()
    {
        String pkSuffix = "_" + System.currentTimeMillis();
        ObjectReferenceDescriptor ord_A = null;
        ObjectReferenceDescriptor ord_B = null;
        ClassDescriptor cld_A = broker.getClassDescriptor(BidirectionalAssociationObjectA.class);
        ord_A = cld_A.getObjectReferenceDescriptorByName("relatedB");
        ClassDescriptor cld_B = broker.getClassDescriptor(BidirectionalAssociationObjectB.class);
        ord_B = cld_B.getObjectReferenceDescriptorByName("relatedA");
        boolean oldA = ord_A.isRefresh();
        boolean oldB = ord_B.isRefresh();
        try
        {
            ord_A.setRefresh(true);
            ord_B.setRefresh(true);
            createWithUpdate(pkSuffix);
            Criteria crit = new Criteria();
            crit.addLike("pk", "%" + pkSuffix);
            Query query = QueryFactory.newQuery(BidirectionalAssociationObjectB.class, crit);
            Collection result = broker.getCollectionByQuery(query);
            assertEquals(1, result.size());
        }
        finally
        {
            if(ord_A != null) ord_A.setRefresh(oldA);
            if(ord_B != null) ord_B.setRefresh(oldB);
        }
    }
View Full Code Here

    public void testShallowAndDeepRetrieval() throws Exception
    {
        String name = "testShallowAndDeepRetrieval_" + System.currentTimeMillis();

        ObjectReferenceDescriptor ord = null;

        try
        {
            // prepare test, create article with ProductGroup
            Article tmpArticle = createArticle(name);
            ProductGroup pg = createProductGroup(name);
            tmpArticle.setProductGroup(pg);
            pg.add(tmpArticle);

            broker.beginTransaction();
            // in repository Article 1:1 refererence to PG hasn't enabled auto-update,
            // so first store the PG. PG has enabled auto-update and will store the
            // article automatic
            broker.store(pg);
            broker.commitTransaction();
            // after insert we can build the Article identity
            Identity tmpOID = broker.serviceIdentity().buildIdentity(tmpArticle);
            broker.clearCache();

            // switch to shallow retrieval
            ClassDescriptor cld = broker.getClassDescriptor(Article.class);
            ord = cld.getObjectReferenceDescriptorByName("productGroup");
            ord.setCascadeRetrieve(false);

            Article article = (Article) broker.getObjectByIdentity(tmpOID);
            assertNull("now reference should be null", article.getProductGroup());

            // now switch to deep retrieval
            ord.setCascadeRetrieve(true);
            // should work without setting cld
            // broker.setClassDescriptor(cld);
            broker.clearCache();
            article = (Article) broker.getObjectByIdentity(tmpOID);
            assertNotNull("now reference should NOT be null", article.getProductGroup());
        }
        finally
        {
            // restore old value
            if(ord != null) ord.setCascadeRetrieve(true);
        }
    }
View Full Code Here

        broker.store(tmpArticle);
        broker.commitTransaction();
        Identity tmpOID = broker.serviceIdentity().buildIdentity(tmpArticle);
        broker.clearCache();

        ObjectReferenceDescriptor ord = null;
        try
        {
            // switch to shallow retrieval
            ClassDescriptor cld = broker.getClassDescriptor(Article.class);
            // article only has one ord
            ord = cld.getObjectReferenceDescriptorByName("productGroup");
            ord.setCascadeRetrieve(false);

            Article article = (Article) broker.getObjectByIdentity(tmpOID);
            assertNull("now reference should be null", article.getProductGroup());

            // now force loading:
            broker.retrieveReference(article, "productGroup");
            assertNotNull("now reference should NOT be null", article.getProductGroup());

            // repair cld
            ord.setCascadeRetrieve(true);
            // should work without setting cld
            // broker.setClassDescriptor(cld);
        }
        finally
        {
            // restore old value
            if(ord != null) ord.setCascadeRetrieve(true);
        }
    }
View Full Code Here

        broker.store(pg);
        broker.store(tmpArticle);
        broker.commitTransaction();
        Identity tmpOID = broker.serviceIdentity().buildIdentity(tmpArticle);
        broker.clearCache();
        ObjectReferenceDescriptor ord = null;
        try
        {
            // switch to shallow retrieval
            ClassDescriptor cld = broker.getClassDescriptor(Article.class);
            ord = (ObjectReferenceDescriptor) cld.getObjectReferenceDescriptors().get(0);
            ord.setCascadeRetrieve(false);

            Article article = (Article) broker.getObjectByIdentity(tmpOID);
            assertNull("now reference should be null", article.getProductGroup());

            // now force loading:
            broker.retrieveAllReferences(article);
            assertNotNull("now reference should NOT be null", article.getProductGroup());

            // clean up cld
            ord.setCascadeRetrieve(true);
        }
        finally
        {
            // restore old value
            if(ord != null) ord.setCascadeRetrieve(true);
        }

    }
View Full Code Here

    /**
     * create either a CollectionPrefetcher or a ReferencePrefetcher
     */
    public RelationshipPrefetcher createRelationshipPrefetcher(ClassDescriptor anOwnerCld, String aRelationshipName)
    {
        ObjectReferenceDescriptor ord;
       
        ord = anOwnerCld.getCollectionDescriptorByName(aRelationshipName);
        if (ord == null)
        {
            ord = anOwnerCld.getObjectReferenceDescriptorByName(aRelationshipName);
View Full Code Here

        if (aTableAlias != null)
        {
            fld = aTableAlias.cld.getFieldDescriptorByName(colName);
            if (fld == null)
            {
                ObjectReferenceDescriptor ord = aTableAlias.cld.getObjectReferenceDescriptorByName(colName);
                if (ord != null)
                {
                    fld = getFldFromReference(aTableAlias, ord);
                }
                else
View Full Code Here

   */
  private TableAlias getTableAlias(String aPath, boolean useOuterJoins, UserAlias aUserAlias, String[] fieldRef, Map pathClasses)
  {
        TableAlias curr, prev, indirect;
        String attr, attrPath = null;
        ObjectReferenceDescriptor ord;
        CollectionDescriptor cod;
        ClassDescriptor cld;
        Object[] prevKeys;
        Object[] keys;
        ArrayList descriptors;
        boolean outer = useOuterJoins;
        int pathLength;
        List hintClasses = null;      
        String pathAlias = aUserAlias == null ? null : aUserAlias.getAlias(aPath);
       
        if (pathClasses != null)
        {
            hintClasses = (List) pathClasses.get(aPath);
        }   
       
        curr = getTableAliasForPath(aPath, pathAlias, hintClasses);
        if (curr != null)
        {
            return curr;
        }

    descriptors = getRoot().cld.getAttributeDescriptorsForPath(aPath, pathClasses);
    prev = getRoot();

    if (descriptors == null || descriptors.size() == 0)
    {
      if (prev.hasJoins())
      {
        for (Iterator itr = prev.iterateJoins(); itr.hasNext();)
        {
          prev = ((Join) itr.next()).left;
          descriptors = prev.cld.getAttributeDescriptorsForPath(aPath, pathClasses);
          if (descriptors.size() > 0)
          {
            break;
          }
        }
      }
    }

    pathLength = descriptors.size();
    for (int i = 0; i < pathLength; i++)
    {
      if (!(descriptors.get(i) instanceof ObjectReferenceDescriptor))
      {
        // only use Collection- and ObjectReferenceDescriptor
        continue;
      }

      ord = (ObjectReferenceDescriptor) descriptors.get(i);
      attr = ord.getAttributeName();
      if (attrPath == null)
      {
        attrPath = attr;
      }
      else
      {
        attrPath = attrPath + "." + attr;
      }

            // use clas hints for path
            if (pathClasses != null)
            {
                hintClasses = (List) pathClasses.get(attrPath);    
            }   

      // look for outer join hint
      outer = outer || getQuery().isPathOuterJoin(attrPath);

      // look for 1:n or m:n
      if (ord instanceof CollectionDescriptor)
      {
        cod = (CollectionDescriptor) ord;
        cld = getItemClassDescriptor(cod, hintClasses);

        if (!cod.isMtoNRelation())
        {
          prevKeys = prev.cld.getPkFields();
          keys = cod.getForeignKeyFieldDescriptors(cld);
        }
        else
        {
          String mnAttrPath = attrPath + "*";
          String mnUserAlias = (aUserAlias == null ? null : aUserAlias + "*");
          indirect = getTableAliasForPath(mnAttrPath, mnUserAlias, null);
          if (indirect == null)
          {
            indirect = createTableAlias(cod.getIndirectionTable(), mnAttrPath, mnUserAlias);

            // we need two Joins for m:n
            // 1.) prev class to indirectionTable
            prevKeys = prev.cld.getPkFields();
            keys = cod.getFksToThisClass();
            addJoin(prev, prevKeys, indirect, keys, outer, attr + "*");
          }
          // 2.) indirectionTable to the current Class
          prev = indirect;
          prevKeys = cod.getFksToItemClass();
          keys = cld.getPkFields();
        }
      }
      else
      {
        // must be n:1 or 1:1
        cld = getItemClassDescriptor(ord, hintClasses);

          // BRJ : if ord is taken from 'super' we have to change prev accordingly
        if (!prev.cld.equals(ord.getClassDescriptor()))
        {
          TableAlias ordAlias = getTableAliasForClassDescriptor(ord.getClassDescriptor());
          Join join = prev.getJoin(ordAlias);
                    if (join != null)
                    {
                        join.isOuter = join.isOuter || outer;
                    }   
            prev = ordAlias;
       

        prevKeys = ord.getForeignKeyFieldDescriptors(prev.cld);
        keys = cld.getPkFields();

        // [olegnitz]
        // a special case: the last element of the path is
        // reference and the field is one of PK fields =>
View Full Code Here

                if (!(key instanceof ObjectReferenceDescriptor))
                {
                    continue;
                }

                ObjectReferenceDescriptor ord = (ObjectReferenceDescriptor) key;
                RelationshipPrefetcher prefetcher;
                ArrayList owners = (ArrayList) entry.getValue();

//                if (ord instanceof SuperReferenceDescriptor || ord.isLazy() || (ord.getItemProxyClass() != null))
                if (ord.isLazy() || (ord.getItemProxyClass() != null))
                {
                    continue;
                }

                prefetcher = pb.getRelationshipPrefetcherFactory().createRelationshipPrefetcher(ord);
View Full Code Here

TOP

Related Classes of org.apache.ojb.broker.metadata.ObjectReferenceDescriptor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.