Using XML configuration files with the pureQuery Generator utility

When you generate an implementation class for an interface that defines annotated methods, you can use an XML file to specify or override the SQL statements that those methods use. You can also use the same XML file to override how the columns in a database object, such as a table or view, map to the properties of a bean.

As the pureQuery Generator creates an implementation for an annotated method in the interface, it checks the XML configuration file for an SQL statement to use if the method is defined with an empty annotation, or for an override of the SQL statement that is used in the annotation of the method. The pureQuery Generator also checks the XML configuration file for any beans that the file might refer to.

To specify an XML configuration file, use the xmlFile option when you invoke the pureQuery Generator.

Example uses for XML configuration files to specify or override SQL statements in interfaces that define annotated methods

The following examples demonstrate situations in which you might want to use XML configuration files.

You might want to override the SQL statements in an interface in situations such as these:
  • You want to use the interface in two different applications that run against two different databases. Rather than include the SQL statements in the interface, you create an XML configuration file for each application, the SQL statements for each application in each XML file.

    For example, you can define an annotated method in the interface like this:

    @Select
    Iterator<Customer> getCustomersInRegion(int r);

    In the XML configuration file for one application, you define the SQL statement that you want to use for this method:

    <named-native-query name="myPackage.CustomerInterface#getCustomersInRegion(int)"> 
      <query>
        <![CDATA[SELECT CUSTID, NAME FROM Customer WHERE STOREREGION=?1]]>
      </query> 
    </named-native-query>

    In the XML configuration file for the other application, you do the same thing:

    <named-native-query name="myPackage.CustomerInterface#getCustomersInRegion(int)"> 
      <query>
        <![CDATA[SELECT ID, NAME FROM CUST WHERE REGION=?1]]>
      </query> 
    </named-native-query>
  • You added SQL statements in the interface and these statements support one application. You now want to use the interface in a second application that runs against a different database. When you generate the implementation class for the second application, you can use an XML file to override the SQL statements that exist in the interface.

    For example, the interface might contain this definition for an annotated method that the first application uses:

    @Select(sql="SELECT CUSTID, NAME FROM Customer WHERE STOREREGION=?1")
    Iterator<Customer> getCustomersInRegion(int r);

    In the XML configuration file for the other application, you can override the SQL like this:

    <named-native-query name="myPackage.CustomerInterface#getCustomersInRegion(int)"> 
      <query>
        <![CDATA[SELECT ID, NAME FROM CUST WHERE REGION=?1]]>
      </query> 
    </named-native-query>

Feedback