包括以下三个必须文件和一个测试用例文件:
Student.java

 1
 2package com.bebig.hibernate.model;
 3
 4/**
 5 * @author fenglin
 6 * @filename Student.java
 7 */

 8public class Student {
 9    private int id;
10
11    public int getId() {
12        return id;
13    }

14
15    public void setId(int id) {
16        this.id = id;
17    }

18
19    public String getName() {
20        return name;
21    }

22
23    public void setName(String name) {
24        this.name = name;
25    }

26
27    public int getAge() {
28        return age;
29    }

30
31    public void setAge(int age) {
32        this.age = age;
33    }

34
35    private String name;
36    private int age;
37
38}

39

Student.hbm.xml,保存到与上一个文件同一文件夹(推荐)
 1<!-- filename:Student.hbm.xml -->
 2<?xml version="1.0"?>
 3<!DOCTYPE hibernate-mapping PUBLIC
 4        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 5        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 6
 7<hibernate-mapping package="com.bebig.hibernate.model">
 8    <class name="Student">
 9        <id name="id">
10            <!-- <generator class="native" /> -->
11        </id>
12        <property name="name"></property>
13        <property name="age"></property>
14    </class>
15</hibernate-mapping>

hibernate.cfg.xml,保存到与src文件夹里:
 1<!-- filename:hibernate.cfg.xml -->
 2<?xml version='1.0' encoding='utf-8'?>
 3<!DOCTYPE hibernate-configuration PUBLIC
 4        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 5        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 6
 7<hibernate-configuration>
 8
 9    <session-factory>
10
11        <!-- Database connection settings -->
12        <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
13        <property name="connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=hibernate</property>
14        <property name="connection.username">sa</property>
15        <property name="connection.password">********</property>
16
17        <!-- JDBC connection pool (use the built-in) -->
18        <!-- <property name="connection.pool_size">1</property> -->
19
20        <!-- SQL dialect -->
21        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
22
23        <!-- Enable Hibernate's automatic session context management -->
24        <!-- <property name="current_session_context_class">thread</property> -->
25
26        <!-- Disable the second-level cache -->
27        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
28
29        <!-- Echo all executed SQL to stdout -->
30        <property name="show_sql">true</property>
31        <property name="format_sql">true</property>
32
33        <!-- Drop and re-create the database schema on startup -->
34         <property name="hbm2ddl.auto">create</property>
35
36        <mapping resource="com/bebig/hibernate/model/Student.hbm.xml" />
37        
38    </session-factory>
39
40</hibernate-configuration>

测试用例:StudentTest.java
 1import org.hibernate.Session;
 2import org.hibernate.SessionFactory;
 3import org.hibernate.cfg.Configuration;
 4
 5import com.bebig.hibernate.model.Student;
 6
 7
 8public class StudentTest {
 9    public static void main(String[] args) {
10        Student s = new Student();
11        s.setId(2);
12        s.setName("Don");
13        s.setAge(26);
14        
15        Configuration cfg = new Configuration(); 
16        SessionFactory sf = cfg.configure().buildSessionFactory();
17        Session session = sf.openSession();
18        session.beginTransaction();
19        session.save(s);
20        session.getTransaction().commit();
21        
22        session.close();
23        sf.close();
24    }
 
25}

26