Using cursors in Oracle ADF.

0

Generating master-detail relationships in Oracle ADF usually is done using two (or more) View Objects which are linked together via a View Link. In a stand-alone application, getting the details for each master row can be a tedious task. First, the next row in the master View Object needs to be gotten, then the View Link relationship needs to be followed to get to the detail View Object over which’s row needs to be iterated. Using a cursor in this case can make life a little easier. Read on to see how we did it.

....

Imagine you’d like to get all Employees in a Department. Using the ADF wizards in JDeveloper, this can easily be set up. Create the Entity Objects for Department and Employee, create an Association between the two, create View Objects for Departments and Employees and finally set up a View Link between Departments and Employees. Next, the ADF layers will take care of everything for you and you’re done.

In some cases it is necessary to iterate over all row in the Departments View Object and get the Employees rows for every Departments row. This can be accomplished by iterating over all Departments rows, get the associated Employees View for every row and iterate over all the Employees rows. Usually this is sufficient, but in some cases it is not.

Enters the cursor. Through a cursor it is possible to get to the detail rows corresponding to a master row. For instance, take this query:

select dep.department_id,<br />       dep.department_name,<br />       cursor (select emp.employee_id,<br />                      emp.first_name,<br />                      emp.last_name<br />               from employees emp<br />               where emp.department_id = dep.department_id) AS emps<br />from departments dep <br />

This will select the ids and department_names from all departments and the ids, first_names and last_names of all employees in the departments. So, how can this be used in ADF?

The trick is to define an attribute in the Departments View Object using the View Object Editor in JDeveloper. Simply name the attribute "EmpsList" and make it of type "oracle.jdbc.OracleResultSet". In the Query Column section, enter as expression

cursor (select emp.employee_id,<br />               emp.first_name,<br />               emp.last_name<br />        from employees emp<br />        where emp.department_id = dep.department_id) <br />

This will make the cursor available as a JDBC Resultset. Since in the current versions of JDBC a ResultSet cannot live apart from the query statement populating it, we’ll use a little trick to store the data in the ResultSet temporarily in another Data Structure.

First we’ll need to define a POJO that may look like this:

package nl.amis.blog.util;<br /><br />public class MyEmp {<br /><br />  private int id;<br />  private String firstName;<br />  private String lastName;<br /><br />  public int getId() {<br />    return id;<br />  }<br /><br />  public void setId(int id) {<br />    this.id = id;<br />  }<br /><br />  public String getFirstName() {<br />    return firstName;<br />  }<br /><br />  public void setFirstName(String firstName) {<br />    this.firstName = firstName;<br />  }<br /><br />  public String getLastName() {<br />    return lastName;<br />  }<br /><br />  public void setLastName(String lastName) {<br />    this.lastName = lastName;<br />  }<br />} <br />

Next, create ANOTHER attribute called Emps and make it of type java.util.ArrayList. Finally, make sure the ViewRowImpl java source file for the Departments View Object is created. Edit the source and make the getEmps() look like this

  public ArrayList getEmps()<br />  {<br />    ArrayList emps = new ArrayList();<br />    OracleResultSet set = getEmpsList();<br />    try {<br />      while (set.next()) <br />      {<br />      	MyEmp myEmp = new MyEmp();<br />        myEmp.setId(set.getInt(1));<br />        myEmp.setId(set.getString(2));<br />        myEmp.setId(set.getString(3));<br />        emps.add(myEmp);<br />      }<br />      //set.beforeFirst();<br />    } catch (Exception e) <br />    {<br />      e.printStackTrace();<br />    }<br />    return emps;<br />  } <br />

 

Instead of referring to the column indices, it’s of course also possible to use the column names. The getEmps method will populate an ArrayList with the data retrieved via the ResultSet. Please note this means a database roundtrip is necessary every time the getEmps method is called. On the other hand, making code like this may appeal to developers who favour writing Java code the "good old way" over learning the ins and outs of yet another framework. The choice is yours.

Share.

About Author

Comments are closed.