Monday, June 25, 2012

Get JPA Entity Attributes programmatically in managed bean

Sometimes it is necessary to get database table field column names for further functional process in applications. In one my previous article I have explained one of the use case - Configure Comparison of Row Objects at Run Time, here application module(BC4J) is used to access the ViewObject attributes.

In EJB data control doesn't create any application module or neither have access to application module Api's directly. When the ViewObject is accessed based on the Iterator a dummy object is created using DCDataVo Api. DCDataVo provides little leverage to access certain model layer functionality, so that all Adapter Data Control can use the DCDataVo api to extend to build custom features.

You can download the sample workspace from here
[Runs with Oracle JDeveloper 11.1.2.0.0 (11g R2) + HR Schema]

Here in the below code JPA entity attributes are accessed in managed bean using DCDataVo api.

/**
 * This method will get the View Object based on Iterator
 * Reads the entity attributes(column names)
 * @param actionEvent
 */
public void compareAttributes(ActionEvent actionEvent) {
 DCBindingContainer dcBindings = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
 DCIteratorBinding deptIter = dcBindings.findIteratorBinding("employeesFindAllIterator");
 ViewObject vo = deptIter.getViewObject();
 int count = 0;
 AttributeDef[] attrDefs = vo.getAttributeDefs();
 for (AttributeDef attrDef : attrDefs) {
  byte attrKind = attrDefs[count].getAttributeKind();
  //Condition to exclude the fk attributes
  if (attrKind == 1) {
   System.out.println(attrDef.getName());
   count++;
  }
 }
}

Displaying the Employee entity attributes.

No comments:

Post a Comment