You can combine the annotated methods in multiple interfaces
by extending those interfaces with another interface. When you perform
a bind on the extending interface, the workbench creates a DB2® package that contains the SQL
statements from the extended interfaces.
You extend interfaces with the extends keyword.
You also add the @PureQueryInterface annotation
to the extending interface.
The extending interface can also declare annotated methods. In
this case, the implementation class that you generate for the extending
interface contains definitions of the annotated methods that are declared
in that interface, as well as definitions of the annotated methods
that are declared in the extended interfaces.
Example
Suppose that you have the following
interfaces:
public interface SalesTeam {
@Select(sql="SELECT * FROM SALES")
List<SalesBean> getSalesEmployees();
}
public interface MarketingTeam {
@Select(sql="SELECT * FROM MARKETING")
List<MktBean> getMarketingEmployees();
}
You want to bind the SQL statements in these interfaces
into a distinct DB2 package.
You can do so by following these steps:
- Create an interface that extends SalesTeam and MarketingTeam.
For this example, the name of the interface is CombinedTeam and
the interface looks like this:
@PureQueryInterface
public interface CombinedTeam extends SalesTeam, MarketingTeam {
}
- Add the @PureQueryInterface annotation
to the CombinedTeam interface.
- Generate the implementation for the CombinedTeam interface
by saving the interface. The implementation for CombinedTeam contains
the methods that are declared in the SalesTeam and MarketingTeam interfaces.
Those interfaces cannot contain any identical annotated methods.
The
workbench does not generate implementations for the SalesTeam and MarketingTeam interfaces.
If implementation classes for those interfaces exist in the project,
the workbench does not delete them. If you make any changes to the SalesTeam or MarketingTeam interfaces,
the workbench regenerates the implementation class for the CombinedTeam interface.
Six possible scenarios
These scenarios show
the behavior of the workbench when you extend interfaces, remove extensions
from interfaces, or delete extending interfaces.
Each scenarios
shows what happens if the Treat generated implementations
as user code check box is or is not selected. By default,
this check box is selected, which means that the workbench never deletes
implementation classes.
You can get to this check box by right-clicking
your Java™ project and selecting Properties.
In the Properties window, expand pureQuery and
select Properties.
These scenarios use
the following interfaces:
- Interface A, which does not declare annotated methods.
- Interfaces B, Y, and Z, which do declare annotated methods.
- SCENARIO 1: Extending interfaces with an interface
that declares no annotated methods
- What you do
- Create interfaces Y and Z. These interfaces declare annotated
methods.
- Create interface A so that it extends interfaces Y and Z.
- What the workbench does if you did specify to retain implementation
classes
- Retains the implementation classes for interfaces Y and Z, if
any exist. If you modify either of these interfaces, the workbench
retains the corresponding implementation class.
- Generates the implementation classes for interface A. This class
defines the annotated methods that are declared in interfaces Y, and
Z.
- What the workbench does if you did not specify to retain implementation
classes
- Deletes the implementation classes for interfaces Y and Z, if
any exist. Any edits that you might have made to these classes are
lost.
- Generates the implementation class for interface A. This class
defines the annotated methods that are declared in interfaces Y and
Z.
- SCENARIO 2: Extending interfaces with an interface
that declares annotated methods
- What you do
- Create interfaces Y and Z. These interfaces declare annotated
methods.
- Create interface B so that it declares annotated methods and extends
interfaces Y and Z.
- What the workbench does if you did specify to retain implementation
classes
- Retains the implementation classes for interfaces Y and Z, if
any exist. However, if you modify either of these interfaces, the
workbench deletes the corresponding implementation class.
- Generates the implementation classes for interface B. This class
defines the annotated methods that are declared in interfaces B, Y,
and Z.
- What the workbench does if you did not specify to retain implementation
classes
- Deletes the implementation classes for interfaces Y and Z, if
any exist. Any edits that you might have made to these classes are
lost.
- Generates the implementation class for interface B. This class
defines the annotated methods that are declared in interfaces Y and
Z.
- SCENARIO 3: Unextending interfaces from an interface
that declares no annotated methods
- What you do
- Edit interface A so that it no longer extends interfaces Y and
Z or no longer contains the @PureQueryInterface annotation.
- What the workbench does if you did specify to retain implementation
classes
- Retains the implementation class for interface A
- What the workbench does if you did not specify to retain implementation
classes
- Deletes the implementation class for interface A, because the
interface does not declare any annotated methods. Any edits that you
might have made to this class are lost.
In both cases, if you want the workbench to generate
implementation classes for interfaces Y and Z, you must perform one
of the following steps:
- Select Project and ensure that the option Build
Automatically is selected. Edit and save the interfaces.
Your edit can be a trivial change, such as adding a whitespace character.
- Select Project > Clean to
build your project.
- SCENARIO 4: Unextending interfaces from an interface
that declares annotated methods
- What you do
- Edit interface B so that it no longer extends interfaces Y and
Z or no longer contains the @PureQueryInterface annotation.
- What the workbench does, whether or not you specified to retain
implementation classes
- Regenerates the implementation class for interface B. Any edits
that you might have made to the implementation class are lost. In
both cases, if you want the workbench to generate implementation classes
for interfaces Y and Z, you must perform one of the following steps:
- Select Project and ensure that the option Build
Automatically is selected. Edit and save the interfaces.
Your edit can be a trivial change, such as adding a whitespace character.
- Select Project > Clean to
build your project.
- SCENARIO 5: Deleting an interface that does not declare
annotated methods and extends interfaces that do
- What you do
- Delete interface A, which does not declare annotated methods and
extends interfaces Y and Z.
- What the workbench does if you did specify to retain implementation
classes
- Retains the implementation class for interface A. However, the
project now contains a compilation error.
- What the workbench does if you did not specify to retain implementation
classes
- Deletes the implementation class for interface A.
In both cases, if you want the workbench to generate
implementation classes for interfaces Y and Z, you must perform one
of the following steps:
- Select Project and ensure that the option Build
Automatically is selected. Edit and save the interfaces.
Your edit can be a trivial change, such as adding a whitespace character.
- Select Project > Clean to
build your project.
- SCENARIO 6: Deleting an implementation class that
declares annotated methods and extends interfaces that do, too
- What you do
- Delete interface B, which declares annotated methods and extends
interfaces Y and Z.
- What the workbench does if you did specify to retain implementation
classes
- Retains the implementation class for interface B. However, the
project now contains a compilation error.
- What the workbench does if you did not specify to retain implementation
classes
- Deletes the implementation class for interface B.
In both cases, if you want the workbench to generate
implementation classes for interfaces Y and Z, you must perform one
of the following steps:
- Select Project and ensure that the option Build
Automatically is selected. Edit and save the interfaces.
Your edit can be a trivial change, such as adding a whitespace character.
- Select Project > Clean to
build your project.