正 文

Java项目中使用Hibernate处理数据


www.7dspace.com  更新日期:2006-2-14 17:20:15  七度空间


  创建和持久化Product

  现在我们终于用到Hibernate了。使用的场景非常简单:

   1. 创建一个有效的Product。

   2. 在应用程序启动时使用net.sf.hibernate.cfg.Configuration获取net.sf.hibernate.SessionFactory。

   3. 通过调用SessionFactory#openSession(),打开net.sf.hibernate.Session。

   4. 保存Product,关闭Session。

  正如我们所看到的,这里没有提到JDBC、SQL或任何类似的东西。非常令人振奋!下面的示例遵循了上面提到的步骤:

package test;

import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import test.hibernate.Product;

// 用法:
// java test.InsertProduct name amount price
public class InsertProduct { 

    public static void main(String[] args) 
                        throws Exception { 

        // 1. 创建Product对象
        Product p = new Product();
        p.setName(args[0]);
        p.setAmount(Integer.parseInt(args[1]));
        p.setPrice(Double.parseDouble(args[2])); 

        // 2. 启动Hibernate
        Configuration cfg = new Configuration()
                         .addClass(Product.class);
        SessionFactory sf = cfg.buildSessionFactory(); 

        // 3. 打开Session
        Session sess = sf.openSession(); 

        // 4. 保存Product,关闭Session
        Transaction t = sess.beginTransaction();
        sess.save(p);
        t.commit();
        sess.close();
    }
}

  让我们来运行它!通过运行java test.InsertProduct Milk 100 1.99命令,插入价格为1.99的100瓶牛奶。我们会得到如下的输出日志:

Nov 23, 2003 9:05:50 AM net.sf.hibernate.cfg.Environment <clinit>
INFO: Hibernate 2.0.3
Nov 23, 2003 9:05:50 AM net.sf.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
Nov 23, 2003 9:05:50 AM net.sf.hibernate.cfg.Environment <clinit>
INFO: using CGLIB reflection optimizer
Nov 23, 2003 9:05:50 AM net.sf.hibernate.cfg.Environment <clinit>
INFO: JVM proxy support: true
Nov 23, 2003 9:05:50 AM net.sf.hibernate.cfg.Configuration addClass
INFO: Mapping resource: test/hibernate/Product.hbm.xml
Exception in thread "main" net.sf.hibernate.MappingException: 
Resource: test/hibernate/Product.hbm.xml not found
    at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:285)
    at test.FindProductByName.main(FindProductByName.java:24)

  它无法工作。其中有两行尤其让人感兴趣:

INFO: hibernate.properties not found and
Resource: test/hibernate/Product.hbm.xml not found.

   当然,INFO行指出我们需要一个hibernate.properties配置文件。在这个文件中,我们配置要使用的数据库、用户名和密码以及其他选项。使用下面提供的这个示例来连接前面提到的Hypersonic数据库:

hibernate.connection.username=sa
hibernate.connection.password=
hibernate.connection.url=jdbc:hsqldb:/home/davor/hibernate/orders
hibernate.connection.driver_class=org.hsqldb.jdbcDriver
hibernate.dialect=net.sf.hibernate.dialect.HSQLDialect

  适当地进行修改(例如,可能需要修改hibernate.connection.url),并保存到classpath中。

  这很容易,但那个test/hibernate/Product.hbm.xml资源是什么呢?它是一个XML文件,定义了Java对象如何被持久化(映射)到一个数据库。在该文件中,我们定义数据存储到哪个数据库表中,哪个字段映射到数据库表的哪个列,不同的对象如何互相关联,等等。让我们来看一下 Product.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="test.hibernate.Product" 
           table="products"> 

        <id name="id" type="string" 
            unsaved-value="null">
            <column name="id" sql-type="char(32)" 
                    not-null="true"/>
            <generator class="uuid.hex"/>
        </id>
        <property name="name">
            <column name="name" sql-type="char(255)" 
                    not-null="true"/>
        </property>
        <property name="price">
            <column name="price" sql-type="double" 
                    not-null="true"/>
        </property>
        <property name="amount">
            <column name="amount" sql-type="integer" 
                    not-null="true"/>
        </property>        
    </class>
</hibernate-mapping>

  它非常简单且易于理解。几个细节特别令人感兴趣:

    * <class name="test.hibernate.Product" table="products">,指出正在映射一个名为test.hibernate.Product的类到表products。

    * <id>元素及其子元素,定义Java类与数据库之间的连接。

    * <property>元素,定义每个字段存储到哪个列及其类型、名称等。

  <generator class="uuid.hex"/>元素乍一看不太好理解。但是知道了它是<id>的一个子元素后,它的作用就很明显了:由于应用程序不知道它的数据如何被持久化(我们一直这么说),我们需要一个没有任何业务含义的代理键帮助Hibernate操纵对象。新创建的Products没有那个id, Hibernate将为我们创建它们。我们选择使用UUID字符串,但它提供了许多ID生成器(顺序的、限定范围的,甚至是用户指派的,等等),而且还可以编写自己的ID生成器。

  现在,创建(复制、粘贴)Product.hbm.xml的内容,并把文件和test.hibernate.Product类放到同一个包内(例如,放置 Product.java文件的目录),重新运行java test.InsertProduct Milk 100 1.99命令。现在我们看到更多的日志以及...没有其他东西了!它运行正常吗?在Session sess = sf.openSession(); 前和sess.close()后添加System.out.println(p),看一看Produc出了什么问题。重新运行程序。您将看到类似于如下内容的日志输出(ID数字肯定会不同的):

[Product] Milk(null) price=1.99 amount=100
[Product] Milk(40288081f907f42900f907f448460001) price=1.99 amount=100

  Hibernate为我们创建了Product的id!让我们看一下Product是否存储到了数据库中。执行select * from products,数据库返回类似于以下内容的输出:

ID                              |NAME  |PRICE |AMOUNT |
40288081f907f42900f907f448460001|Milk  |1.99  |100    |

  Product信息被成功地插入到了数据库中,我们甚至都还没有编写一行SQL语句!

  插入一些其他产品,例如面包、咖啡、啤酒等,这样就可以继续学习下面的教程。

6页,页码:[1] [2] [3] [4] [5] [6] 

上一篇:聊天更轻松 腾讯QQ消息自动发送
下一篇:情人节个性礼品光盘制作全攻略
标题:Java项目中使用Hibernate处理数据 作者:Davor Cengija 来源:bea
收藏此页】【打印】【关闭
站 内 搜 索
 

热 点 导 读
特 别 推 荐