使用Hibernate 连接SQL Server 2000


以下代码在 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");

本文作者:
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3