Hibernate 3学习笔记 - 简单的Persistent class和mapping file

 
首先我们来看一个简单的Hibernate持久化的类:
import java.util.Date;
 
public class Event {
private Long id;
private String title;
private Date date;
 
public Event() {}
 
public Long getId() {
return id;
}
 
private void setId(Long id) {
this.id = id;
}
 
public Date getDate() {
return date;
}
 
public void setDate(Date date) {
this.date = date;
}
 
public String getTitle() {
return title;
}
 
public void setTitle(String title) {
this.title = title;
}
}
 
大家可以看到,这就是一个简单的POJO. 这个例子遵循了标准的JavaBean的命名习惯,所有的成员变量都是private的,每个成员变量都有相应的getter,setter方法。实际上这并不是Hibernate的要求,Hibernate也可以直接访问成员变量(property)。Hibernate可以访问public,protected和private的方法,也可以直接访问public,protected,private的成员变量。开发人员可以依据实际情况决定使用那种方式。
在上面的例子中,id成员变量代表的是可以唯一确定一个Event实例的属性(通常来说是数据库表中的主键)。Hibernate要求持久化的类都必须有这样的成员变量(并不是一定要叫id)。
同时,Hibernate还要求持久化的类必须有一个无参数的构造函数,以便Hibernate利用反射来生成该类的实例。该构造函数可以是private的,但是如果要生产运行时的代理,或者要考虑代码的效率,至少要保证包一级的可见(也就是protected)。
只有持久化的类还不够,Hibernate必须要知道怎样保存和载入持久化类的对象。Hibernate使用映射文件(mapping file)来定义这些信息。mapping file会告诉Hibernate使用数据库中的什么表和表中的哪些列,以及这些列和类中的成员变量的对应关系。Mapping file的基本格式如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
[...]
</hibernate-mapping>
对于以上的Event类,相应的Mapping file如下:
<hibernate-mapping>
<class name="events.Event" table="EVENTS">
<id name="id" column="EVENT_ID">
               <generator class="native"/>
</id>
<property name="date" type="timestamp" column="EVENT_DATE"/>
<property name="title"/>
</class>
</hibernate-mapping>
 
在这里,class定义了java类与数据库表的对应关系。其中name属性是java类的全限定名(包含包名的类名)。table属性是数据库中的表名(如有必要,可以包含schema的名称)。
class的子元素id定义了可以唯一标识这个类的成员变量(前面提过了,Hibernate要求每个持久化类都包含一个这样的成员变量)。name属性指明了该成员变量的名字,column属性指明了该成员变量对应表的哪个column。generator元素指明了生成该ID的方法。这里native表明由Hibernate来决定生成主键的方式,Hibernate会根据使用的数据库的类型来选择最合适的生产主键的方式。
每个property元素对应Event类中的一个成员变量。property元素的name属性指明了Event中的成员变量名称;type属性告诉Hibernate该成员变量的类型;Column属性告诉Hibernate该成员变量对应数据库表中的哪个Column。type属性使用的并不是Java中的数据类型,也不是SQL的数据库数据类型,而是Hibernate自己定义的,叫做Hibernate mapping type(Hibernate映射类型)。该类型定义了从Java数据类型到SQL数据类型的对应关系。
在上面的mapping file中,没有直接指明title成员变量对应的数据类型和column的名称,这样Hibernate会使用缺省值。对于column,Hibernate会使用成员变量的名称作为column的名称。对于type,Hibernate会根据Java类中该成员变量的类型来决定使用哪种Hibernate mapping type。
 
附:
  1. id的generator可以为以下的值:
  • increment :generates identifiers of type long, short or int that are unique only when no other process is inserting data into the same table. Do not use in a cluster.
  • identity:supports identity columns in DB2, MySQL, MS SQL Server, Sybase and HypersonicSQL. The returned identifier is of type long, short or int.
  • sequence :uses a sequence in DB2, PostgreSQL, Oracle, SAP DB, McKoi or a generator in Interbase. The returned identifier is of type long, short or int
  • hilo :uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a table and column (by default hibernate_unique_key and next_hi respectively) as a source of hi values. The hi/lo algorithm generates identifiers that are unique only for a particular database.
  • seqhilo :uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a named database sequence.
  • uuid :uses a 128-bit UUID algorithm to generate identifiers of type string, unique within a network (the IP address is used). The UUID is encoded as a string of hexadecimal digits of length 32.
  • guid :uses a database-generated GUID string on MS SQL Server and MySQL.
  • native:picks identity, sequence or hilo depending upon the capabilities of the underlying database.
  • assigned :lets the application to assign an identifier to the object before save() is called. This is the default strategy if no <generator> element is specified.
  • select :retrieves a primary key assigned by a database trigger by selecting the row by some unique key and retrieving the primary key value.
  • foreign :uses the identifier of another associated object. Usually used in conjunction with a <one-to-one> primary key association.
  • sequence-identity :a specialized sequence generation strategy which utilizes a database sequence for the actual value generation, but combines this with JDBC3 getGeneratedKeys to actually return the generated identifier value as part of the insert statement execution. This strategy is only known to be supported on Oracle 10g drivers targetted for JDK 1.4. Note comments on these insert statements are disabled due to a bug in the Oracle drivers.
 
  1. type可以为以下的值:
  • Hibernate基本类型,例如 integer, string, character, date, timestamp, float, binary, serializable, object, blob.
  • Java中有缺省的基本类型的类,例如int, float, char, java.lang.String, java.util.Date, java.lang.Integer, java.sql.Clob.
  • 实现了Seriliazable接口的Java类.
  • 用户自定义的类型的类名。(关于用户自定义的类型,以后会学习到)。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值