コード例 #1
0
ファイル: BrokerExamples.java プロジェクト: cniesen/ojb-1.0.4
  /** tests the PB.retrieveAllReferences() feature */
  public void testRetrieveAllReferences() {
    String name = "testRetrieveAllReferences_" + System.currentTimeMillis();

    // ensure there is an item to find
    Article tmpArticle = createArticle(name);
    ProductGroup pg = createProductGroup(name);
    tmpArticle.setProductGroup(pg);

    broker.beginTransaction();
    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);
    }
  }
コード例 #2
0
ファイル: BrokerExamples.java プロジェクト: cniesen/ojb-1.0.4
  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);
    }
  }
コード例 #3
0
ファイル: BrokerExamples.java プロジェクト: cniesen/ojb-1.0.4
  /** tests the PB.retrieveReference() feature */
  public void testRetrieveReference() throws Exception {
    String name = "testRetrieveReference_" + System.currentTimeMillis();

    // ensure there is an item to find
    Article tmpArticle = createArticle(name);
    ProductGroup pg = createProductGroup(name);
    tmpArticle.setProductGroup(pg);
    broker.beginTransaction();
    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);
      // 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);
    }
  }