Hibernate入门篇之新增特性

版本:hibernate-2.1final。

eclipse 2.1.2。

jsdk-1.4.2

所有文件存放在:com.javamodel.hibernate目录下

hibernate在连接数据库方面有两种方式:

1。使用hibernate.properties文件,我下面采用的就是这种方式。这种方式比较直观,也符合一些老程序员的思维模式。

2。使用hibernate.cfg.xml文件,这是hibernate默认的文件,在这里可以描述mapping文件,我想这就是最大的好处吧。

好,让我们开始吧!

Example.java

package com.javamodel.hibernate;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.MappingException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;

public class Example{
    
    private static SessionFactory _sessions = null;
    private static Properties pops = new Properties();
    //启动后,一次加载。
    static{
        try {
                //这里根据个人的喜好,在这里要是着这样的话:"/hibernate.properties",
                //就可以把hibernate.properties放到src目录下和com平级,在tomcate中放在web应用程序的web-inf/class目录下了。
            InputStream stream = Example.class.getResourceAsStream("hibernate.properties");
            try {
                pops.load(stream);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            Configuration cfg = new Configuration();
            //比较麻烦的地方,class需要一个一个的加载。也就是增加一个表和对应的类,那各类要在这里注册一下。
            cfg.addClass(Person.class);
            cfg.setProperties(pops);
            _sessions = cfg.buildSessionFactory();
        } catch (MappingException e) {
           e.printStackTrace();
        } catch (HibernateException e) {
           e.printStackTrace();
        }
    
    }
    
    public static void main(String[] args) throws HibernateException {
        
        Person person = new Person();
        person.setName("smallduzi");
        person.setEmail("smallduzi@sohu.com");
        
        Session session = _sessions.openSession();
        
        Transaction tx = null;
        try{
            tx = session.beginTransaction();
            session.save(person);
            tx.commit();        
        }catch(HibernateException he){
            if(tx != null) tx.rollback();
            throw he;
        }
        finally{
            session.close();
        }
        
    }
    
}

Person.java

package com.javamodel.hibernate;

public class Person {
    
    private String id = null;
    private String name = null;
    private String email = null;    
    
    public Person(){}

    /**
     * @return
     */
    public String getEmail() {
        return email;
    }

    /**
     * @return
     */
    public String getId() {
        return id;
    }

    /**
     * @return
     */
    public String getName() {
        return name;
    }

    /**
     * @param string
     */
    public void setEmail(String string) {
        email = string;
    }

    /**
     * @param string
     */
    public void setId(String string) {
        id = string;
    }

    /**
     * @param string
     */
    public void setName(String string) {
        name = string;
    }

}

Person.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
    <class name="com.javamodel.hibernate.Person" table="person">
        <id name="id">
            <column name="id" length="40"/>
            <generator class="uuid.hex"/>
        </id>
        <property name="name" column="name" />
        <property name="email" column="email" />
    </class>
</hibernate-mapping>

这里我用的是oracle数据库,如果你使用的是其他数据库,可参照hibernate.properties中其他的数据库连接说明。

hibernate.properties

## Oracle

hibernate.dialect net.sf.hibernate.dialect.OracleDialect
hibernate.connection.driver_class oracle.jdbc.driver.OracleDriver
hibernate.connection.username XXX
hibernate.connection.password XXX
#hibernate.connection.url jdbc:oracle:thin:@192.168.0.28:1521:orcl
hibernate.connection.url jdbc:oracle:oci8:@XXX

我没有对他进行进一步的封装。那样初学者可能不理解。对session的封装,可以参照hibernate文档的HibernateUtil.java这个类(没记错的话)。

注意:在工程里要引用hibernate的jar。参照hibernate的文档中有说需要的必须的jar。

posted @ 2004-12-16 12:57  wuxh  阅读(177)  评论(0)    收藏  举报