The sign on the View Accessors tab in JDeveloper says "View accessors are used for defining and filtering the data source referenced in list based validation and presented in a list of values". And really, usually we define view accessors for LOVs and, may be, sometimes, for list validators. But besides that view accessors can be used as a convenient way to execute a SQL query while implementing some business logic. Since business logic is concentrated at the entity level and usually is implemented in the EntityImpl class it is convenient to define and work with a view accessor at the entity level.
Let's consider a simple use case. There is a standard Employees entity. In order to implement some business logic we need to calculate an average salary for the employee's job in the employee's department. So, we need to execute the following SQL query:
We have defined a view object based on this query (just definition without any instance in AM) and defined a corresponding view accessor in the Employees entity:
For the view objects like this, that are used for some calculations only, it is recommended to setup Forward Only access mode at the Tuning section of the General tab:
JDeveloper generated a getter method for the view accessor in the EmployeesImpl class:
And whenever we need to get an updated result of this query in our Java code we can do tho following:
That's it!
Let's consider a simple use case. There is a standard Employees entity. In order to implement some business logic we need to calculate an average salary for the employee's job in the employee's department. So, we need to execute the following SQL query:
select AVG(Salary) avg_salary from Employees where department_id = :departmentid and job_id = :jobid
We have defined a view object based on this query (just definition without any instance in AM) and defined a corresponding view accessor in the Employees entity:
For the view objects like this, that are used for some calculations only, it is recommended to setup Forward Only access mode at the Tuning section of the General tab:
JDeveloper generated a getter method for the view accessor in the EmployeesImpl class:
/** * Gets the view accessor <code>RowSet</code> VAVGSalary. */ public RowSet getVAVGSalary() { return (RowSet) getAttributeInternal(VAVGSALARY); }
And whenever we need to get an updated result of this query in our Java code we can do tho following:
public BigDecimal getCalcAvgSalary() { Row r = getVAVGSalary().first(); return (r!=null ? (BigDecimal) r.getAttribute(0) : null); }This method can be used in a Groovy expression as well. For example as a default value expression of some transient attribute:
That's it!
No comments:
Post a Comment
Post Comment