如果你觉得Hibernate开发,数据库表映射关系的XML不想写,那就可以使用Annotation来开发,它的配置很简单,由于Hibernate3.5以上版本将Annotation直接集成到Hibernate Core里了,因此,你只需要在Build Path将hibernate-distribution-3.5.6-Final\lib\jpa\hibernate-jpa-2.0-api-1.0.0.Final.jar包加入,即可使用Annotation(Annotation是基于JPA标准的一个框架项目)。

下面是一个实例:
Teacher.java
 1package com.bebig.hibernate.model;
 2
 3import java.util.Date;
 4
 5import javax.persistence.Entity;
 6import javax.persistence.EnumType;
 7import javax.persistence.Enumerated;
 8import javax.persistence.Id;
 9import javax.persistence.Temporal;
10import javax.persistence.TemporalType;
11
12//注解为实体类
13@Entity
14public class Teacher {
15
16    private int id;
17    private String name;
18    private String title;
19    private Date birthDate;
20    private Status status;
21     
22    //指定主键
23    @Id
24    public int getId() {
25        return id;
26    }

27
28    public void setId(int id) {
29        this.id = id;
30    }

31
32    public String getName() {
33        return name;
34    }

35
36    public void setName(String name) {
37        this.name = name;
38    }

39
40    public String getTitle() {
41        return title;
42    }

43
44    public void setTitle(String title) {
45        this.title = title;
46    }

47
48    public void setBirthDate(Date birthDate) {
49        this.birthDate = birthDate;
50    }

51
52    @Temporal(TemporalType.DATE)
53    public Date getBirthDate() {
54        return birthDate;
55    }

56
57    public void setStatus(Status status) {
58        this.status = status;
59    }

60
61    @Enumerated(EnumType.ORDINAL)
62    public Status getStatus() {
63        return status;
64    }

65
66}

67

Status.java,以上最后一个字段用到的枚举类型数据:
1package com.bebig.hibernate.model;
2
3public enum Status {
4    sNormal,sAbnormal
5}

6

hibernate.cfg.xml
 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        <mapping class="com.bebig.hibernate.model.Teacher"/>
36
37    </session-factory>
38
39</hibernate-configuration>

用JUnit创建的测试用例,TeacherTest.java
 1package com.bebig.hibernate.model;
 2
 3import java.util.Date;
 4
 5import org.hibernate.HibernateException;
 6import org.hibernate.Session;
 7import org.hibernate.SessionFactory;
 8import org.hibernate.cfg.AnnotationConfiguration;
 9import org.junit.AfterClass;
10import org.junit.BeforeClass;
11import org.junit.Test;
12
13public class TeacherTest {
14    private static SessionFactory sf = null;
15
16    @BeforeClass
17    public static void beforeClass() {
18        try {
19            sf = new AnnotationConfiguration().configure().buildSessionFactory();
20        }
 catch (HibernateException e) {
21            // TODO Auto-generated catch block
22            e.printStackTrace();
23        }

24    }

25
26    @Test
27    public void TestTeacherSave() {
28        Teacher t = new Teacher();
29        t.setId(2);
30        t.setName("Scan");
31        t.setTitle("高级");
32        t.setBirthDate(new Date());
33        t.setStatus(Status.sNormal);
34
35        Session session = sf.openSession();
36        session.beginTransaction();
37        session.save(t);
38        session.getTransaction().commit();
39
40        session.close();
41
42    }

43    
44    public static void main(String[] args)
45    {
46        beforeClass();
47    }

48
49    @AfterClass
50    public static void afterClass() {
51        sf.close();
52    }

53}

54