public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
}
Hibernate对属性使用的类型不加限制。所有的Java JDK 类型和原始类型(比如 String,char和
float)都可以被映射,也包括 Java 集合框架(Java collections framework)中的类。你可以把
它们映射成为值,值集合,或者与其他实体相关联。id是一个特殊的属性,代表了这个类的数据
库标识符(主键),它对于类似于 Cat这样的实体是必需的。
持久化类不需要实现什么特别的接口,也不需要从一个特别的持久化根类继承下来。Hibernate
也不需要使用任何编译期处理,比如字节码增强操作,它独立的使用Java反射机制和运行时类增
强(通过 CGLIB)。所以,在Hibernate中,POJO的类不需要任何前提条件,我们就可以把它映射
成为数据库表。
1.3. 映射cat
Cat.hbm.xml映射文件包含了对象/关系映射所需的元数据。
原数据包含了持久化类的声明和把它与其属性映射到数据库表的信息(属性作为值或者是指向其
他实体的关联)。
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="net.sf.hibernate.examples.quickstart.Cat" table="CAT">
<!-- A 32 hex character is our surrogate key. It's automatically
generated by Hibernate with the UUID pattern. -->
<id name="id" type="string" unsaved-value="null" >
<column name="CAT_ID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<!-- A cat has to have a name, but it shouldn' be too long. -->
<property name="name">
<column name="NAME" sql-type="varchar(16)" not-null="true"/>
</property>
<property name="sex"/>