Java Reference
In-Depth Information
the mapping file, you can dictate exactly how changes to an entity should be enforced. The
disadvantage is that you will lose Hibernate's guarantee of cross-database platform portability.
The advantage is that you can carry out operations that are not explicitly described in the
mapping, such as calculating and inserting values in the process of carrying out an insert.
The tags are <sql-insert> , <sql-update> , and <sql-delete> . All three work in the same way.
If you take a look at the DDL script for this appendix, you will see that our client table
includes seven fields, the last of which is the country field, as shown in Listing A-16.
Listing A-16. The DDL Script to Create the Client Table
create table client (
id int not null primary key,
name varchar(32) not null,
number varchar(10),
streetname varchar(128),
town varchar(32),
city varchar(32),
country varchar(32)
);
We will, however, ignore the country field in our mapping file. We would like this to be
automatically set to UK whenever a client entity is persisted.
Depending on the database, this could be implemented as part of the view's support for
writing operations, or as a trigger invoked when the other fields are written—but we use the
<sql-insert> tag to specify the operation to perform.
The necessary ordering of the parameters can be determined by running Hibernate with
logging enabled for the org.hibernate.persister.entity level. You must do this before you
add the mapping. Listing A-17 shows a suitably formatted <sql-insert> element with the
parameters suitably ordered. Note that the identifier field id is in the last position—not the
first, as you might have expected.
Listing A-17. The Mapping File Using Explicit SQL to Update the Tables
<class name="com.hibernatebook.legacy.Client" table="Client">
<id type="int" name="id" column="id">
<generator class="native"/>
</id>
<property type="text" name="name" column="name"/>
<property type="text" name="number" column="number"/>
<property type="text" name="streetname" column="streetname"/>
<property type="text" name="town" column="town"/>
<property type="text" name="city" column="city"/>
<sql-insert>
insert into client(name,number,streetname,town,city,id,country)
values (?,?,?,?,?,?,'UK');
</sql-insert>
</class>
Search WWH ::




Custom Search