Java Reference
In-Depth Information
Listing A-15. The Revised Mapping
<class name="com.hibernatebook.legacy.Product" table="vwProduct">
<composite-id
class="com.hibernatebook.legacy.ProductKey"
name="key">
<key-property
type="int"
name="id"
column="ProductKey"/>
<key-property
type="int"
name="colorId"
column="ColorKey" />
</composite-id>
<property
name="id"
type="int"
column="id"
insert="false"
update="false"
unique="true"/>
<property
name="SKU"
type="int"
column="sku"
insert="false"/>
<many-to-one
name="color"
class="com.hibernatebook.legacy.Color"
column="ColorId"/>
</class>
The behavior of the composite primary key is unchanged, but the SKU now becomes
a simple property. The color entity is mapped as before.
The caveat for this approach is the problem of writing data to the mapping. Some
databases (for example, versions 4 and lower of MySQL) do not support writable views, and
others may have only limited support for them. To avoid views in these circumstances, we
must abandon complete portability in favor of database-specific SQL inserted directly into
the mapping file.
Putting SQL into a Mapping
Hibernate provides three tags that can be used to override the default behavior when writing
to the database. Instead of accepting the SQL generated by Hibernate from the information in
Search WWH ::




Custom Search