Getting FindByPrimaryKey to Find Extended Entity Instances, Too

Send me a mail
 Dive into Oracle ADF   Click to see the XML version of this web page.   (Updated: 2/3/2008; 9:25:51 PM.)
Tips and tricks from Steve Muench on Oracle ADF Framework and JDeveloper IDE

Getting FindByPrimaryKey to Find Extended Entity Instances, Too

I've had a couple of different people ask in the last week about ADF-related issues that both boiled down to Bug 3267441, which reports that doing a findByPrimaryKey() on a parent entity object definition -- like Person -- doesn't find an entity instance if it happens to be of a subtype of that entity -- like Doctor extends Person. The following code illustrates the easy workaround in case you need to have this functionality in your ADF application. It's also part of the PolymorphicVORows example I built yesterday for a customer who was asking for one that showed off all of our polymorphism features in ADF Business Components. As illustrated in the example, to make use of a customized Entity Definition class implementation, just provide your class name instead of the default one for the "Definition Class" which you can set by clicking on the (Class Extends...) button on the "Java" panel of the Entity Object editor. Like the other undocumented examples, when I get a chance I'll be turning them into more well-documented OTN HowTo's in the future.

package example.fwkext;
import com.sun.java.util.collections.Iterator;
import com.sun.java.util.collections.List;
import oracle.jbo.Key;
import oracle.jbo.server.DBTransaction;
import oracle.jbo.server.EntityDefImpl;
import oracle.jbo.server.EntityImpl;
public class CustomEntityDefImpl extends EntityDefImpl  {
  /**
   * Workaround for Bug 3267441:
   * FindByPrimaryKey On Parent Entity Doesn't Find Extended Child EO Instance
   * -------------------
   * Overridden framework method to search in the entity caches
   * of subtyped entity defs if the findByPrimaryKey() returns
   * null for the current entity def.
   * @param txn DBTransaction to use
   * @param key Key of the entity instance being looked-up
   * @return Entity instance if found, or null of not found.
   */
  public EntityImpl findByPrimaryKey(DBTransaction txn, Key key) {
    EntityImpl e = super.findByPrimaryKey(txn, key);
    if (e == null) {
      List extendedDefs = getExtendedDefObjects();
      if (extendedDefs != null) {
        Iterator iter = extendedDefs.iterator();
        while (iter.hasNext()) {
          e = ((EntityDefImpl)iter.next()).findByPrimaryKey(txn,key);
          if (e != null) {
            break;
          }
        }
      }
    }
    return e;
  }
}



© Copyright 2008 Steve Muench. Click here to send an email to the editor of this weblog.
Last update: 2/3/2008; 9:25:51 PM.