Lazy property fetching in Java O/R Mapping
Property can be fetched lazily from the database when it is first accessed. It can be useful if the table contains many columns and most of them are not being read frequently.
Lazy property fetching requires build-time bytecode instrumentation; othewise, the lazy property settings will be ignored.
Defining lazy properties
To define a property as lazy, open the attribute specification by right-clicking on the attribute and selecting
Open Specification... from the popup menu.
Switch to the
ORM Attribute Detail page and check the
Lazy checkbox.

Generating code
Generate ORM code as usual. On the menu, select
Tools > Object-Relational Mapping (ORM) > Generate Code...
Specify the code generation options and database options, click the
OK button to generate code.

Running post-compilation
As mentioned above, lazy property fetching requires build-time bytecode instrumentation. This can be done by running the script file which is produced by ORM code generation with the batch, shell, or ant script option checked. When the project contains lazy properties, the generated script will perform instrumentation automatically after compilation. The Ant script will contains a new default target instrument, so there is no change in the deployment process to enable lazy property fetching unless an explicit target is specified before.
For SDE users, note that lazy property fetching will not work if the code is compiled by the IDE without instrumentation. Therefore, you need to do the instrumentation after the compilation. You can run the generated Ant script build-dbva.xml with the default target or instrument target for instrumentation.
Checking if lazy property is applied
In this example, the class Customer contains 4 attributes : ID, name, age, address.
address is defined as lazy, while others are non-lazy.
To illustrate the effect of lazy property fetching, we can turn on the show sql option to see which columns are fetched from the database. To turn on this option, edit “Output Path\src\ormmapping\ProjectName.cfg.xml”, search for the line <property name=”show_sql”>false</property> and change false to true.
Now, compile and run the ‘create data’ and ‘list data’ sample. You’ll see the following output if you didn’t override toString() method to access address property:
select
customer0_.ID as ID0_,
customer0_.name as name0_,
customer0_.age as age0_
from
Customer customer0_
You can see that the address property is not fetched from database because it has never been accessed, but all other non-lazy properties are fetched immediately. Let’s try to modify the ‘list data’ sample and add the following line of code:
System.out.println(lmodelCustomers[i].getAddress());
Compile and run the ‘list data’ sample again. In addition to the previous output, you’ll see the following:
select
customer_.address as address0_
from
Customer customer_
where
customer_.ID=?
You can see that the address property is fetched from database only when it is being accessed.
Downloads
VP-UML Project file
Complete source
Resources
DB Visual ARCHITECT Home
DB Visual ARCHITECT Quick Tour
Programmer’s guide for Java
Download DB Visual ARCHITECT