China Open source community
站内导航:

 
 
 
当前位置: 首页 >> 程序设计 >> 使用Hibernate处理数据
 

使用Hibernate处理数据

作者:Davor Cengija      来源:dev2dev.bea.com.cn/techdoc/20060208730.html     发表时间:2006-04-16     浏览次数:      字号:    

启动Hibernate

  在我们假想的应用程序中,基本的使用模式非常简单:我们将创建一个Product,然后将其持久化(或者换句话说,保存它);我们将搜索并加载一个已经持久化的Product,并确保其可以使用;我们将会更新和删除Product。

创建和持久化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语句!
插入一些其他产品,例如面包、咖啡、啤酒等,这样就可以继续学习下面的教程。

查找和加载产品

  查找和加载已经持久化的对象在Hibernate中非常简单。使用它的查询语言,我们可以很容易地通过ID、名称或其他属性获取一个对象(或对象集)。我们能够获取完整的对象或它的一部分属性。Hibernate将处理余下的工作,最后,我们将拥有相当有用的对象层次体系。我们来看一下test.FindProductByName类。

package test;

import java.util.List;

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

// 用法:
// java test.FindProductByName name
public class FindProductByName {

    public static void main(String[] args) throws Exception {
        // 执行的查询
        String query =
            "select product from product "
            + "in class test.hibernate.Product "
            + "where product.name=:name";

        // 搜索的内容
        String name = args[0];

        // 初始化
        Configuration cfg = new Configuration()
                           .addClass(Product.class);

        SessionFactory sf = cfg.buildSessionFactory();

        // 打开会话
        Session sess = sf.openSession();
        
        // 搜索并返回
        List list = sess.find(query, name, 
                              Hibernate.STRING);

        if (list.size() == 0) {
            System.out.println("No products named " 
                               + name);
            System.exit(0);
        }
        Product p = (Product) list.get(0);
        sess.close();
        System.out.println("Found product: " + p);
    }
}

[1] [2] [3]

编辑 webmaster

 
 
 
评论
 
 
发表
 
姓名: QQ:
性别: MSN:
E-mail: 主页:
评分: 1 2 3 4 5
评论内容:
验证码:
  
  • 请遵守《互联网电子公告服务管理规定》及中华人民共和国其他各项有关法律法规。
  • 严禁发表危害国家安全、损害国家利益、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的评论 。
  • 用户需对自己在使用本站服务过程中的行为承担法律责任(直接或间接导致的)。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表网友个人观点,与本网站立场无关。
  •  
    中国源码网 - WWW.YUANMA.ORG - 中国开放源代码社区