Using ORM Qualifier
ORM Qualifier allows you to specify extra data retrieval rules apart from the system pre-defined rules. With ORM Qualifier, you can define retrieval rules that match your business logic. Let's say your program requires retrieving staff records for specific gender. Without using ORM Qualifier, you will need to retrieve all records, and check record by record to find out the staff that match the gender you look for. With ORM Qualifier, you can retrieve data with specialized methods like listByGender(char gender). This not only makes the code looks tidier but also reduce the chance of making error.
Defining ORM Qualifier in object model
In order to use ORM Qualifier you need to define the qualifier(s) in your object model by selecting the attribute(s) to be included in qualifier.
- Right click on the ORM Persistable class in which you want to define an ORM Qualifier.
Open class specification - Open the ORM Query tab.
- Click Add... in the ORM Qualifiers section.
Add a ORM Qualifier - In the ORM Qualifier Specification window, enter the name of the ORM Qualifier and select the key attribute. The name you defined will be appended to method names in generated code. For example, an ORM qualifier named Gender will result in generating methods like loadByGender(...), listByGender(...), etc.
Defining an ORM Qualifier - Click OK to confirm.
- Click OK to return to diagram.
ORM Qualifier (generated code)
ORM Qualifier methods will be generated in Persistent class according to the selected Persistent API. For example, if you selected Factory class as Persistent API, then the following methods will be generated in the StaffFactory class.
|
||||||||||||||||||||
Methods of a typical ORM Qualifier class |
Using ORM Qualifier in programming
You can use the qualifier methods to load or list data from database. The following examples show how to load or list via ORM qualifier.
Load
By executing the code the FIRST occurrence of 'm' gender column in the Staff table will be loaded to a Staff object. Here is the result:
List
for (int i = 0; i < staffs.length; i++){
System.out.println(staffs[i]);
}
By executing the code, ALL rows that contain 'f' in the gender column in the Staff table will be retrieved and stored in an array of Staff object.
Staff[ Name=Peggy Age=45 Gender=f Dob=1960-11-07 ID=3 ]
Related Resources
The following resources may help you to learn more about the topic discussed in this page.
4. PersistentManager and Transaction | Table of Contents | 6. Using ORM Criteria |