论坛首页 Java版 Hibernate

JBX + Sqlserver2k 快速上手例子一个(使用hibernate.proper...

浏览 5645 次
该帖已经被评为精华帖
作者 正文
最后更新时间:2004-04-07
最近才开始潜心研究hibernate的使用,刚在看入门的材料,帖子很多,但对我需要的都不算太清楚。
或者是讲oracle的,不是sql server的,或者不是用JB的,更或者不是配置hibernate.properties的,而是配置那个xml文件的,对熟悉的人,这些都不是问题,但对初学者,这都是问题。
经过我摸索,在多人的帖子的基础上,终于能做成一个完整的例子了。现共享一下。

环境:
开发的IDE:JBuilderX
使用的数据库:MS Sql Server 2000
使用的数据库驱动:JSQL Driver(JDBC 3.0)

说明:
1、hibernate在配置文件中明确说明“Microsoft Driver (not recommended!)”,因此先使用JSQL Driver。
2、JSQL Driver可以到http://www.jnetdirect.com中得到,需要先注册个用户,才能下载到试用的版本。
3、JDBC3.0只能在JDK1.4及以上版本中使用,JBuilderX默认的是JDK1.4

准备工作:
1、下载Hibernate,目前最高版本是2.1.2
2、在JBuilder中创建一个lib,起名为hibernate_full,将hibernate\lib下的所有jar通通放进去,并将hibernate\hibernate2.jar也放进去
3、在JBuilder中创建一个lib,起名为JSQL3,将JSQL Driver下的JNetDirect\JSQLConnect\JDBC_3.0_Driver\JSQLConnect.jar放进去
   
最后更新时间:2004-04-08
开始进行例子
1、创建一个project,命名为testhibernate
2、在属性里的Required Libraries里加入hibernate_full和JSQL3
3、在菜单Project --> Project Properties --> Build --> Resource 里选中xml文件,选择“Copy” --在编译该项目的时候,会自动将src文件夹里的xml文件拷贝到classes文件夹里的相应目录下
4、在testhibernate项目中创建一个src目录
5、将hibernate源文件里的hibernate\src\hibernate.properties 和 log4j.properties拷贝到testhibernate项目中的src目录下

6、修改hibernate.properties中关于MS Sql Server 2000驱动方面的配置
找到
[code:1]## HypersonicSQL

hibernate.dialect net.sf.hibernate.dialect.HSQLDialect
hibernate.connection.driver_class org.hsqldb.jdbcDriver
hibernate.connection.username sa
hibernate.connection.password
hibernate.connection.url jdbc:hsqldb:hsql://localhost
hibernate.connection.url jdbc:hsqldb:test
hibernate.connection.url jdbc:hsqldb:.[/code:1]
这段,这里是说默认的是使用HypersonicSQL,我们使用的是MS Sql Server,因此将整段注释掉
[code:1]## HypersonicSQL

#hibernate.dialect net.sf.hibernate.dialect.HSQLDialect
#hibernate.connection.driver_class org.hsqldb.jdbcDriver
#hibernate.connection.username sa
#hibernate.connection.password
#hibernate.connection.url jdbc:hsqldb:hsql://localhost
#hibernate.connection.url jdbc:hsqldb:test
#hibernate.connection.url jdbc:hsqldb:.[/code:1]

并且,找到
[code:1]## MS SQL Server

#hibernate.dialect net.sf.hibernate.dialect.SQLServerDialect
#hibernate.connection.username sa
#hibernate.connection.password sa

## JSQL Driver
#hibernate.connection.driver_class com.jnetdirect.jsql.JSQLDriver
#hibernate.connection.url jdbc:JSQLConnect://1E1/test[/code:1]
这段,比如我们使用的数据库服务器机器名为yuj,数据库名为testhi,连接到数据库上去的用户名为sa,密码为sa,则修改后这段成为
[code:1]## MS SQL Server

hibernate.dialect net.sf.hibernate.dialect.SQLServerDialect
hibernate.connection.username sa
hibernate.connection.password sa

## JSQL Driver
hibernate.connection.driver_class com.jnetdirect.jsql.JSQLDriver
hibernate.connection.url jdbc:JSQLConnect://yuj/testhi[/code:1]
   
0 请登录后投票
最后更新时间:2004-04-07
7、创建一个类testhibernate.Person,这是个标准的JavaBean,只有3个属性和相应的get\set方法
[code:1]package testhibernate;

public class Person
{
  private String id;
  private String name;
  private String address;

  public void setId(String value)
  {
    this.id = value;
  }

  public String getId()
  {
    return id;
  }

  public void setName(String value)
  {
    this.name = value;
  }

  public String getName()
  {
    return name;
  }

  public void setAddress(String value)
  {
    this.address = value;
  }

  public String getAddress()
  {
    return address;
  }
}[/code:1]

8、创建一个对象-关系映射的xml文件Person.hbm.xml,放在和Person.java相同的目录下面
[code:1]<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class name="testhibernate.Person">
    <!--hibernate为我们生成主键id-->
<id name = "id" unsaved-value = "null">
<generator class="uuid.hex"/>
</id>

    <!--默认把类的变量映射为相同名字的表列,当然我们可以修改其映射方式-->
<property name="name"/>
<property name="address"/>
</class>
</hibernate-mapping>[/code:1]

9、创建调用类Person的客户端程序Client1.java
[code:1]
package testhibernate;

import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;

/**
*本类只是用来创建表的,并不往表内部插入任何数据,并且只能使用一次,否则会删除已有的表的
*/
public class Client1
{
  private static SessionFactory sessionFactory;

  public static void main(String[] args) throws Exception
  {
    Configuration conf = new Configuration().addClass(Person.class);

    //第一次运行时用来在数据库中创建表
    //并且把sql语句输出到txt文件用的
    //以后的运行不能使用该段代码,否则每次都会先删除原表,再新建该表
    SchemaExport dbExport = new SchemaExport(conf);
    dbExport.setOutputFile("sql.txt");
    dbExport.create(true, true);
  }
}[/code:1]
   
0 请登录后投票
最后更新时间:2004-04-07
在这个时候make整个项目,然后run Client1,没有报错的话,打开MS SQL SERVER的企业管理器进入其中查看数据库中是否创建了一张表,名为person,如果有,则整个过程就是成功的了,下面再用另一个类Client2来创建两条记录。
   
0 请登录后投票
最后更新时间:2004-04-07
[code:1]package testhibernate;

import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;

public class Client2
{
  private static SessionFactory sessionFactory;

  public static void main(String[] args) throws Exception
  {
    Configuration conf = new Configuration().addClass(Person.class);
    sessionFactory = conf.buildSessionFactory();
    Session s = sessionFactory.openSession();

    Transaction t = s.beginTransaction();

    Person yuj = new Person();
    yuj.setName("john");
    yuj.setAddress("上海");

    Person x = new Person();
    x.setName("zhaoyh");
    x.setAddress("上海");

    //持久化
    s.save(yuj); //此时yuj已经可以在数据库中找到
    s.save(x);    //此时x已经可以在数据库中找到

    t.commit();
    s.close();
  }
}[/code:1]

查看数据库中,增加了2条记录,OK!初步使用成功了,剩下的慢慢研究吧……
   
0 请登录后投票
最后更新时间:2005-04-20
好啊好啊  谢谢  可是为么子我在JBuilder2005
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;

这个地方会报错package net.sf.hibernate does not exits呢? 
这个net.sf从哪里来的?
请解答 初学者在这里谢谢了
   
0 请登录后投票
最后更新时间:2005-04-20
如果用的hibernate3以上版本,把net.sf改成org
   
0 请登录后投票
最后更新时间:2005-04-20
搞出来了  我用的Hibernate3.0 

相应的位置改成了
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
就可以了好像  赫赫    谢谢了

可惜还是不晓得那个 对象-关系映射的xml文件 怎么写啊  3.0的和2.0的不一样写吧?
   
0 请登录后投票
最后更新时间:2005-04-21
3.0 的DTD 是不一样的
   
0 请登录后投票
论坛首页 Java版 Hibernate

跳转论坛:
JavaEye推荐