EBS OAF开发中的Java 实体对象(Entity Object)(一)(四)

2014-11-24 02:36:11 · 作者: · 浏览: 2
ey()的同样的检查。然后你可以选择在一个单独的步骤对数据库做同样的检查。这个方法的好吃就是你可以避免不必要的对象实例化。

下面的示例也是来自于ToolBox Tutorial SupplierEOImpl类:

public void setName(String value)

{

  // Since this value is marked as "mandatory," the BC4J Framework will take

  // care of ensuring that it's a non-null value. However, if it is null, we

  // don't want to proceed with any validation that could result in a NPE.

   

  if ((value != null) || (!("".equals(value.trim()))))

  {

    // Verify that the name is unique. To do this, we must check both the entity

    // cache and the database. We begin with the entity cache.

    com.sun.java.util.collections.Iterator supplierIterator = 

      getEntityDef().getAllEntityInstancesIterator(getDBTransaction());

   

    Number currentId = getSupplierId();

   

    while ( supplierIterator.hasNext() )

    {

      SupplierEOImpl cachedSupplier = (SupplierEOImpl)supplierIterator.next();

 

      String cachedName = cachedSupplier.getName();

      Number cachedId = cachedSupplier.getSupplierId();

      

      // We found a match for the name we're trying to set, so throw an

      // exception. Note that we need to exclude this EO from our test.

 

      If (cachedName != null && value.equalsIgnoreCase(cachedName) &&    

        cachedId.compareTo(currentId) != 0 )

      {

        throw new OAAttrValException(OAException.TYP_ENTITY_OBJECT,

                               getEntityDef().getFullName(), // EO name

                               getPrimaryKey(), // EO PK

                               "Name", // Attribute Name

                               value, // Attribute value

                               "ICX", // Message product short name

                               "FWK_TBX_T_SUP_DUP_NAME"); // Message name 

      }

    }

     

    // Now we want to check the database for any occurrences of the supplier

    // name. The most efficient way to check this is with a validation view

    // object which we add to a special "Validation" application module.

    OADBTransaction transaction = getOADBTransaction();

    OAApplicationModule vam;

    // Look to see if the VAM has already been created in this transaction. If not,

    // create it.

    vam = (OAApplicationModule)transaction.findApplicationModule("supplierVAM");

    if (vam == null)

    { 

      vam = 

      (OAApplicationModule)transaction.createApplicationModule("supplierVAM",

         "oracle.apps.fnd.framework.toolbox.schema.server.SupplierVAM");

    }

   

    // Now, we use a lightweight "validation" view object to see if a supplier exists

    // with the given name.

    SupplierNameVVOImpl valNameVo = (SupplierNameVVOImpl)vam.findViewObject("SupplierNameVVO");

    valNameVo.initQuery(value);

    if (valNameVo.hasNext())

    {

      throw new OAAttrValException(OAException.TYP_ENTITY_OBJECT,

            getEntityDef().getFullName(), // EO name

            getPrimaryKey(), // EO PK

            "Name", // Attribute Name

            value, // Attribute value

            "ICX", // Message application short name

            "FWK_TBX_T_SUP_DUP_NAME"); // Message name

    }

  }

   

  setAttributeInternal(NAME, value);

} // end setName()


关联迭代

这意义而言就和findByPrimaryKey()类似,它保证了会即检查实体缓存和数据库。它也会加载它在数据库中找到的实体对象到内存中,如果你想在实体对象上调用方法的话,它就比较有用。不像findByPrimaryKey(),它可以使用任意键来查找任意类型的实体对象,这个方法仅用于取出以关联关系关联当前对象的实体对象。

下面的示例表明了如何使用根组合实体对象的关联来查找它所有的子对象。

private void checkLineExists()

{ 

  // A purchase order header must have at least 1 associated line.

  // To check this, we first do a manual check of the entity cache

  // If we find a line for this header, we're done (note that the entity cache won't

  // include EOs that are DELETED or DEAD).

 

  com.sun.java.util.collections.Iterator fastIterator = 

    PurchaseOrderLineEOImpl.getDefinitionObject().getAllEntit