HIBERNATE是一个数据库层持久框架,今天下载了一个3.3.1版试用了一下;
首先创建一个对象类:
import java.util.Date;

public class Event {
  private Long id;

  private String title;
  private Date date;

  public Event() {
  }

  public Long getId() {
    return id;
  }

  private void setId(Long id) {
    this.id = id;
  }

  public Date getDate() {
    return date;
  }

  public void setDate(Date date) {
    this.date = date;
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }
}
接下来创建相应对象的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">
<hibernate-mapping>
  <class name="events.Event" table="EVENTS">
    <id name="id" column="EVENT_ID">
      <generator class="native" />
    </id>
    <property name="date" type="timestamp" column="EVENT_DATE" />
    <property name="title" />
  </class>
</hibernate-mapping>
接下来就是创建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>

                <!-- Database connection settings -->
    <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
    <property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
    <property name="connection.username">sa</property>
    <property name="connection.password"></property>

                <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

                <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.HSQLDialect</property>

                <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

                <!-- Disable the second-level cache    -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

                <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>
    <!--
      Drop and re-create the database schema on startup <property
      name="hbm2ddl.auto">create</property>
    
-->
    <mapping resource="events/Event.hbm.xml" />
  </session-factory>
</hibernate-configuration>
这里对于数据则采用了纯JAVA实现的HSQLDB。
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

  private static final SessionFactory sessionFactory;

  static {
    try {
      // Create the SessionFactory from hibernate.cfg.xml
      // 此处还可以在configure方法中指定具体的配置文件 configure(File configFile)
      sessionFactory = new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
      // Make sure you log the exception, as it might be swallowed
      System.err.println("Initial SessionFactory creation failed." + ex);
      throw new ExceptionInInitializerError(ex);
    }
  }
  public static SessionFactory getSessionFactory() {
    return sessionFactory;
  }
}
store:开始往数据库中写入数据
list:读取目前的记录并在控制台打印出来
public class EventManager {

  public static void main(String[] args) {
    EventManager mgr = new EventManager();

    if (args[0].equals("store")) {
      mgr.createAndStoreEvent("My Event", new Date());
    } else if (args[0].equals("list")) {
      List events = mgr.listEvents();
      for (int i = 0; i < events.size(); i++) {
        Event theEvent = (Event) events.get(i);
        System.out.println("Event: " + theEvent.getTitle() + " Time: " + theEvent.getDate());
      }
    }

    HibernateUtil.getSessionFactory().close();
  }

  private List listEvents() {

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();

    session.beginTransaction();

    List result = session.createQuery("from Event").list();

    session.getTransaction().commit();

    return result;
  }

  private void createAndStoreEvent(String title, Date theDate) {

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();

    session.beginTransaction();

    Event theEvent = new Event();
    theEvent.setTitle(title);
    theEvent.setDate(theDate);

    session.save(theEvent);

    session.getTransaction().commit();
  }

}
最终的SRC目录结构如下:
src
│hibernate.cfg.xml
├─events
│Event.hbm.xml
│Event.java
└─util
EventManager.java
HibernateUtil.java
引用的LIB包有:
lib
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate3.jar
hsqldb.jar
javassist-3.4.GA.jar
jta-1.1.jar
slf4j-api-1.5.2.jar
slf4j-simple-1.5.2.jar
当然还需要启动数据库:
F:\dev\hsqldb\mytest>java -classpath ../lib/hsqldb.jar org.hsqldb.Server
F:\dev\hsqldb\mytest>java -cp ../lib/hsqldb.jar org.hsqldb.util.DatabaseManager
Hibernate的JAVADOC文档,没找到下载的链接,只有一个在线链接:[url]http://www.hibernate.org/hib_docs/v3/api/[/url]