You can generate beans from a selection of tables, aliases,
views, or materialized query tables (MQTs) in a database. You can
generate code that uses annotated methods or inline methods.
Before you begin
Depending on your license configuration, this functionality
might not be available.
- In the Package Explorer, create a Java project or use an existing
one.
- In the Data Source Explorer, create a connection
to the database that contains the database objects that you want to
use. If the Data Source Explorer does not currently
appear in the workbench, select . In the Show View window, select .
About this task
Beans are useful for passing input values to annotated
or inline methods for querying or updating the database objects that
the beans are based on.
If you want to use annotated methods, you
can choose to generate the following Java objects:
- An interface that contains abstract methods that are annotated
with the SQL statements for querying and updating the database object
that the generated bean is based on. You must have all of the privileges
that are required for running the SQL statements.
If you choose
to generate this interface, the workbench also generates an implementation
of the interface during a build of the Java project.
- A test class that calls the methods in the implementation and
displays the results in the Console view.
If
you choose not to generate the test class, you can do so at a later
time by right-clicking the bean and selecting . This option also regenerates the related interface
and the implementation of that interface.
If you want to use inline methods, you can
choose to generate a test class that contains inline methods for querying
and updating the database object that the generated bean is based
on. When you run the class, the results are displayed in the Console view.
If you choose not to generate the test class, you can do so at a later
time by right-clicking the bean and selecting .
Procedure
To generate code from multiple tables, aliases, views,
and MQTs:
- In the Data Source Explorer, select all of the tables,
aliases, views, and MQTs that you want to represent as Java beans. To select a sequential
set of objects, click the first object in the set, and then press
and hold down the Shift key while you click the last object in the
set. To select a set of nonsequential objects, click the first object,
and then press and hold down the Ctrl key while you click the remaining
objects in the set.
- Right-click the set of selected objects, and then select . The Generate
pureQuery Code from Tables wizard opens.
- Complete the steps of the wizard. For information
about each field, press F1.
- Click Finish to generate the files
that you specified.
Results
If you did not already add pureQuery support to your Java project, after you click Finish the
workbench creates the folder dataAccessFolder in your Java project.
If
you are using DB2® for z/OS®, or DB2 for Linux, UNIX, or Windows, this folder contains the files Default.genProps and Default.bindProps.
You use these files if you plan to run SQL statements statically from
applications that use annotated methods.
What to do next
If you have SQL queries that join multiple tables, you can
use pureQuery annotations and define beans so that pureQuery Runtime
generates a set of nested beans that replicate the relational structure
of the data in the database. For information about creating beans
for SQL statement that join multiple tables, see
Beans and SQL statements that join tables