Java Reference
In-Depth Information
public void setId(int id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
// We will map the id to the table's primary key
private int id = -1;
// We will map the username into a column in the table
private String username;
}
It's pretty easy to see that we might want to represent the class in Listing 7-3 in a table
with the format shown in Table 7-16.
Table 7-16. Mapping a Simple Class to a Simple Table
Column
Type
Id
Integer
Username
Varchar(32)
The mapping between the two is, thus, similarly straightforward:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
< class name="book.hibernatebook.chapter06.User">
< id name="id" type="int">
< generator class="native"/>
</id>
< property name="username" type="string" length="32"/>
</class>
</hibernate-mapping>
Aside from the very limited number of properties maintained by the class, this is a pretty
common mapping type, so it is reassuring to see that it can be managed with a minimal
number of elements ( <hibernate-mapping> , <class> , <id> , <generator> , and <property> ).
Search WWH ::




Custom Search