以下代码在 jdk 5.0, hibernate 2.1, sql server 2000 sp3 中测试通过。
第一次使用hibernate作持久层,感觉使用起来还是比较复杂的,尤其是调试起来很不方便。hibernate 基于反射的机制虽然很灵活,但明显给跟踪代码制造了障碍,给出的异常信息量也太少。个人感觉其改进的余地还很大,比如java新增加了annotation语法后,是否可使用它来定义orm,而取代hbm.xml的形式。
好了,进入正题。
首先,必须配置数据库,下面以在数据库yufan中的操作为例。
create table customer(cid integer not null primary key, username varchar(12) not null, password varchar(12));
然后是一个数据对象,必须为它的每个字段提供读写属性方法,hibernate 会用反射来检索。
// customer.java
public class customer {
private int id;
private string username;
private string password;
public int getid() {
return id;
}
public string getpassword() {
return password;
}
public string getusername() {
return username;
}
public void setid(int id) {
this.id = id;
}
public void setpassword(string password) {
this.password = password;
}
public void setusername(string username) {
this.username = username;
}
}
然后是hibernate的映射customer.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<!doctype hibernate-mapping public
"-//hibernate/hibernate mapping dtd//en"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="customer" table="customer" proxy="customer">
<id name="id" column="cid">
<generator class="increment"/>
</id>
<property name="username" column="username" />
<property name="password" column="password" />
</class>
</hibernate-mapping>
类和映射结合在一起,定义了orm。
下面是hibernate的配置文件,包含数据库连接,映射文件引用等。文件名必须是hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8" ?>
<!doctype hibernate-configuration
public "-//hibernate/hibernate configuration dtd//en"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory name="java:/hibernate/hibernatefactory">
<property name="show_sql">true</property>
<property name="connection.driver_class">
com.jnetdirect.jsql.jsqldriver
</property>
<property name="connection.url">
jdbc:jsqlconnect://localhost:1433;database=yufan;
</property>
<property name="connection.username">
sa
</property>
<property name="connection.password">
yufan
</property>
<property name="dialect">
net.sf.hibernate.dialect.sqlserverdialect
</property>
<mapping resource="customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
重要的property包括connection.driver_class,指定jdbc数据库驱动。connection.url 制定数据库的url。我使用的是jsqldriver,功能上比ms 的 jdbc 驱动强大。但必须注意的是,在指定数据库的时候,必须使用database= 或 databasename=的语法,而databasename则会出错。ms的驱动则无此问题。我刚遇到此问题时真是茫然无绪。。。最后发现是如此。。。
在这个文件中,不能使用xml注释,感觉是hibernate的缺陷。
最后是test.java
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class test {
public static void main(string[] args) {
try {
sessionfactory sf = new configuration().configure().buildsessionfactory();
session session = sf.opensession();
transaction tx = session.begintransaction();
for (int i = 0; i < 20; i++) {
customer customer = new customer();
customer.setusername("customer" + i);
customer.setpassword("customer");
session.save(customer);
}
tx.commit();
session.close();
}
catch (hibernateexception e) {
e.printstacktrace();
}
}
}
好了,在项目中添加对hibernate库文件的引用,编译执行,你会在数据库中找到新添加的记录。
代码部分取自史上最简单的hibernate入门简介by watano_cc,根据sql server改动。
