EJB Named
criteria are predefined and reusable where-clause definitions that are
dynamically applied to a ViewObject query. Here we often use to filter
the ViewObject SQL statement query based on Where Clause conditions.Take a scenario where we need to filter the SQL statements query based on Where Clause conditions, instead of playing with SQL statements use the EJB Named Criteria which is supported by default in ADF and set the Bind Variable parameter at run time.You can download the sample workspace from here
[Runs with Oracle JDeveloper 11.1.2.0.0 (11g R2) + HR Schema] Implementation StepsCreate Java EE Web
Application with entity based on Employees table, then create a session
bean and data control for the session bean.Open the DataControls.dcx file and create sparse xml for as shown below.
In sparse xml navigate to Named criteria tab -> Bind Variable section, create binding variable deptId.
Now create
a named criteria and map the query attributes to the bind variable.
In the ViewController create index.jspx page, from data control palette
drop employeesFindAll->Named Criteria->EmployeesCriteria->Table as ADF Read-Only Filtered Table and create the backingBean as "IndexBean".Open
the index.jspx page and remove the "filterModel" binding from the
table, add <af:inputText />, command button and bind them to
backingBean. For command button create the actionListener as
"applyEmpCriteria" and add below code to the file.
public void applyEmpCriteria(ActionEvent actionEvent) {
DCIteratorBinding dc = (DCIteratorBinding)evaluteEL("#{bindings.employeesFindAllIterator}");
ViewObject vo = dc.getViewObject();
vo.applyViewCriteria(vo.getViewCriteriaManager().getViewCriteria("EmployeesCriteria"));
vo.ensureVariableManager().setVariableValue("deptId", this.getDeptId().getValue());
vo.executeQuery();
}
/**
* Programmtic evaluation of EL
*
* @param el EL to evalaute
* @return Result of the evalutaion
*/
public Object evaluteEL(String el) {
FacesContext fctx = FacesContext.getCurrentInstance();
ELContext elContext = fctx.getELContext();
Application app = fctx.getApplication();
ExpressionFactory expFactory = app.getExpressionFactory();
ValueExpression valExp = expFactory.createValueExpression(elContext, el, Object.class);
return valExp.getValue(elContext);
}
Run the index.jspx page, enter departmentId value as 90 and click in
ApplyEmpCriteria button. Now the bind variable for the Named criteria
will be applied at runtime in the backing bean and it will re-execute
ViewObject query to filter based on where clause condition.