NetBeans6.5中使用Hibernate的例子

来源:互联网 发布:乐视抢购软件神器 编辑:程序博客网 时间:2024/05/17 01:14

使用MYSQL数据库,创建一个mytest数据库,创建一个person表,字段有

id,name,password,sex,email 都设置为nvarchar类型。方便测试。

在NB中,新建一个JAVA项目

选择JAVA应用程序

起一个过程名字名字:ht01,选定存放目录后,别的不用修改,点完成。

在项目页中的ht01树中,包里,引入Hibernate包,和MYSQL-JDBC连接包.

 

在项目中,新建"Hibernate配置文件",产生一个hibernate.cfg.xml文件,在项目下,空包里。

内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mytest</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
  </session-factory>
</hibernate-configuration>

在项目里新建一个Person包,在这个包里面,新建"Hibernate的映射文件及POJO“

在对话框中,选择 Person表,配置文件选择"Hibernate.cfg.xml"

点下一步,选择包,点完成。这样就在Person包中生成出Person.java文件和Person.hbm.xml文件。

还有一个hibernate.reveng.xml文件.目前不知道这个文件的具体用途,好像不是很重要,删除也不影响。

同时,在hibernate.cfg.xml中的session-factory中自动加入了

<mapping resource="Person/Person.hbm.xml"/>

 

Person.hbm.xml内容为:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2010-2-22 7:16:49 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="Person.Person" table="person" catalog="mytest">
        <id name="id" type="string">
            <column name="id" length="32" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="name" length="20" not-null="true" />
        </property>
        <property name="password" type="string">
            <column name="password" length="20" not-null="true" />
        </property>
        <property name="sex" type="string">
            <column name="sex" length="2" />
        </property>
        <property name="email" type="string">
            <column name="email" length="30" />
        </property>
    </class>
</hibernate-mapping>

 

Person.java是最标准的POJO,内容为:

package Person;
// Generated 2010-2-22 7:16:43 by Hibernate Tools 3.2.1.GA

 

/**
 * Person generated by hbm2java
 */
public class Person  implements java.io.Serializable {


     private String id;
     private String name;
     private String password;
     private String sex;
     private String email;

    public Person() {
    }

 
    public Person(String id, String name, String password) {
        this.id = id;
        this.name = name;
        this.password = password;
    }
    public Person(String id, String name, String password, String sex, String email) {
       this.id = id;
       this.name = name;
       this.password = password;
       this.sex = sex;
       this.email = email;
    }
  
    public String getId() {
        return this.id;
    }
   
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return this.name;
    }
   
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return this.password;
    }
   
    public void setPassword(String password) {
        this.password = password;
    }
    public String getSex() {
        return this.sex;
    }
   
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getEmail() {
        return this.email;
    }
   
    public void setEmail(String email) {
        this.email = email;
    }

 


}

 

下面在ht01.java中写测试代码

在main中加入

        try{


         SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session session = sf.openSession();
        Transaction tx = session.beginTransaction();
        Person hw = new Person();
        hw.setId("id2");
        hw.setName("name1");
        hw.setPassword("password1");
        hw.setSex("1");
        hw.setEmail("email1");
        session.save(hw);
        tx.commit();
        session.close();
        }
        catch(HibernateException e){
           e.printStackTrace();
        }
       

 

添加hibernate及Person的imoprt.

import Person.Person;

import java.io.File;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

 

运行RUN

数据被写入到数据库中

 

 

 

运行中提示如下信息,run:
2010-2-24 3:33:44 org.hibernate.cfg.Environment <clinit>
信息: Hibernate 3.2.5
2010-2-24 3:33:44 org.hibernate.cfg.Environment <clinit>
信息: hibernate.properties not found
2010-2-24 3:33:44 org.hibernate.cfg.Environment buildBytecodeProvider
信息: Bytecode provider name : cglib
2010-2-24 3:33:44 org.hibernate.cfg.Environment <clinit>
信息: using JDK 1.4 java.sql.Timestamp handling
2010-2-24 3:33:44 org.hibernate.cfg.Configuration configure
信息: configuring from file: Hibernate.cfg.xml

成功生成(总时间:2 秒)