Java Reference
In-Depth Information
using an actual interface type instead of an abstract class directly. A good
approach to achieve both code reuse and a separation between interface
and implementation is a tripartite design. In a tripartite design you still use
an abstract class to achieve some level of code reuse, but you design the
abstract class to implement an interface, and then expose only the interface
to the public application programming interface ( API ). Finally, the special-
ized (or concrete) classes extend from the base class and inherently imple-
ment the interface.
6.3.1
Mapping Inheritance
iBATIS supports inheritance hierarchies by using a special mapping called a dis-
criminator. Using a discriminator you can determine the type of class to be instan-
tiated based on a value in the database. The discriminator is a part of the Result
Map and works much like a switch statement. For example:
<resultMap id="document" class="testdomain.Document">
<result property="id" column="DOCUMENT_ID"/>
<result property="title" column="TITLE"/>
<result property="type" column="TYPE"/>
<discriminator column="TYPE" javaType="string" >
<subMap value="Book" resultMap="book"/>
<subMap value="Newspaper" resultMap="news"/>
</discriminator>
</resultMap>
The discriminator above can be read this way:
If the column “ TYPE ” contains the value “Book,” then use the result
map called “book,” otherwise if the column TYPE contains the value
“Newspaper,” then use the result map called “news.”
The sub maps are just normal result maps referenced by name (see below). If the
discriminator can't find a value to match one of the sub maps, then the parent
result map is applied. If an appropriate value is found, then only the sub map is
applied—the parent's defined result mappings are not applied unless the sub
maps explicitly extend the parent map, as you can see in the following example.
<resultMap id="book" class="testdomain.Book" extends="document">
<result property="pages" column="DOCUMENT_PAGENUMBER"/>
</resultMap>
The extends attribute of the result map effectively copies all of the result mappings
from the referenced result map. However, it does not imply anything about the class
hierarchy. Remember, i BATIS is not an object/relational mapping framework per
Search WWH ::




Custom Search