Java Reference
In-Depth Information
In addition to the three SQL terms for writing to the database, you can specify hand-rolled
SQL for reading. This is appended as <sql-query> tags outside the class tag (see Listing A-18).
They are not intrinsically a part of the mapping. However, you can specify that one of them
should be used as the default loader for your class.
Listing A-18. An Alternative Mapping File Defining a Default Loader
...
<hibernate-mapping>
<class name="com.hibernatebook.legacy.Client"
table="Client">
<id type="int" name="id" column="id">
<generator class="native"/>
</id>
<property name="name"/>
<property name="number"/>
<property name="streetname"/>
<property name="town"/>
<property name="city"/>
<loader query-ref="DefaultQuery"/>
</class>
<sql-query name="DefaultQuery">
<return alias="c"
class="com.hibernatebook.legacy.Client"/>
SELECT
id as {c.id},
'NOT SPECIFIED' as {c.name},
number as {c.number},
streetname as {c.streetname},
town as {c.town},
city as {c.city}
FROM
Client
WHERE
id = ?
</sql-query>
</hibernate-mapping>
n Tip Unfortunately, this technique is not quite as sophisticated as you might hopeā€”the custom SQL will
not be invoked in general terms. Only if the id is explicitly supplied, as is the case when calling the Session
class's get() method, will the default handling be overridden in favor of the loader query.
Search WWH ::




Custom Search