Advanced Database Persistence for Java

Examples & Reference Manual

 

Custom DAOs

The <sequence> and <query> tags can be included in existing <table> and <view> tags that serve as container DAO classes where to place the corresponding Java method that retrieve sequence values and execute SQL updates respectively.

This was though to consider most of the time the <sequence> and <query> tags are conceptually related to a database table or view. If this is the case then the corresponding <table> and <view> tags seems to best container to place them.

However, there are <sequence> and <query> tags that are not conceptually related to any table or view. The simplest workaround is to just place them into any existing <table> or <view> tag. Yet this is not a clean solution.

HotRod provides a container DAO classes to be defined using the <dao> tag. These DAOs are unrelated to any database object and serve only as container for <sequence> and <query> tags. In essence, they are heavily simplified <table> tags that do not relate to any table and do not have any persistence method except for the enumerated <sequence> and <query> tags.

The <dao> Tag

The <dao> tag attributes are:

Attribute Description Required
java-class-name The name of the DAO Java class that will be created. The name must be unique and different to all other names produced from a <table>, <view>, <select>, and/or <dao> tag since they live in the same namespace. Yes

The example below shows a custom DAO definition:

<dao java-class-name=”DocumentUtilities”>

  <sequence name="document_seq" />

<dao java-class-name=ā€¯DocumentUtilitiesā€¯>

  <sequence name="document_seq" />

  <query java-method-name="markOldDocuments">
  <![CDATA[
    update document set mark = 1
      where mark = 0 and closing_date
        between #{from,javaType=java.util.Date,jdbcType=TIMESTAMP}
            and #{to,javaType=java.util.Date,jdbcType=TIMESTAMP}
  ]]>
  </query>

</dao>

The DAO above defines the DAO Java class DocumentUtilities . This is a DAO class (even if the specified name does not include the DAO suffix). The execution of its methods will participate in any transaction as all DAOs do.

The name specified on the <dao> tag must be different to all other DAOs produced from a <table> , <view> , <select> , and/or <dao> tag since they live in the same namespace.

This DAO class has two static methods: the selectSequenceDocumentSeq() method retrieves sequence values, and the markOldDocuments(from, to) that executes the parameterized SQL update.