第一步:准备hibernate.cfg.xml文件(也可以找到log4j.properties,它与Hibernate配置文件在同一个目录中),可以在源码中找个模板(路径为:hibernate-3.2.0.ga\hibernate-3.2\doc\tutorial\src\..)
第二步:准备表对象和映射文件(也就是类和相应的*.hbm.xml文件,在同一个包下),源代码中的模板路径为:\hibernate-3.2.0.ga\hibernate-3.2\doc\tutorial\src\events\..
第三步:准备程序入口即main函数,并导入需要用到的包,下面是一个精简的列表(只需要11个包):
antlr.jar
asm.jar
cglib-2.1.3.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
hibernate3.jar
jta.jar
junit-3.8.1.jar
log4j-1.2.14.jar
ojdbc14.jar
下面是一个例子的源代码:
1,配置文件hibernate.cfg.xml
1 <?xml version='1.0' encoding='utf-8'?>
2 <!DOCTYPE hibernate-configuration PUBLIC
3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5
6 <hibernate-configuration>
7
8 <session-factory>
9
10 <!-- Database connection settings -->
11 <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
12 <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property>
13 <property name="connection.username">scott</property>
14 <property name="connection.password">tiger</property>
15
16 <!-- SQL dialect -->
17 <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
18
19 <!-- Enable Hibernate's automatic session context management -->
20 <property name="current_session_context_class">thread</property>
21
22 <!-- Echo all executed SQL to stdout -->
23 <property name="show_sql">true</property>
24
25 <!-- Drop and re-create the database schema on startup -->
26 <property name="hbm2ddl.auto">create</property>
27
28 <mapping resource="com/klein/Animal.hbm.xml"/>
29
30 </session-factory>
31
32 </hibernate-configuration>
log4j.properties
1 ### direct log messages to stdout ###
2 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
3 log4j.appender.stdout.Target=System.out
4 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
5 log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
6
7 ### direct messages to file hibernate.log ###
8 #log4j.appender.file=org.apache.log4j.FileAppender
9 #log4j.appender.file.File=hibernate.log
10 #log4j.appender.file.layout=org.apache.log4j.PatternLayout
11 #log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
12
13 ### set log levels - for more verbose logging change 'info' to 'debug' ###
14
15 log4j.rootLogger=warn, stdout
16
17 log4j.logger.org.hibernate=info
18 #log4j.logger.org.hibernate=debug
19
20 ### log HQL query parser activity
21 #log4j.logger.org.hibernate.hql.ast.AST=debug
22
23 ### log just the SQL
24 #log4j.logger.org.hibernate.SQL=debug
25
26 ### log JDBC bind parameters ###
27 log4j.logger.org.hibernate.type=info
28 #log4j.logger.org.hibernate.type=debug
29
30 ### log schema export/update ###
31 log4j.logger.org.hibernate.tool.hbm2ddl=debug
32
33 ### log HQL parse trees
34 #log4j.logger.org.hibernate.hql=debug
35
36 ### log cache activity ###
37 #log4j.logger.org.hibernate.cache=debug
38
39 ### log transaction activity
40 #log4j.logger.org.hibernate.transaction=debug
41
42 ### log JDBC resource acquisition
43 #log4j.logger.org.hibernate.jdbc=debug
44
45 ### enable the following line if you want to track down connection ###
46 ### leakages when using DriverManagerConnectionProvider ###
47 #log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
48
2, 映射文件Animal.hbm.xml
1 <?xml version="1.0"?>
2 <!DOCTYPE hibernate-mapping PUBLIC
3 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
5
6 <hibernate-mapping>
7 <class name="com.klein.Animal" table="t_animal">
8 <id name="id">
9 <generator class="native"/>
10 </id>
11 <property name="name"/>
12 </class>
13 </hibernate-mapping>
表对象Animal
1 package com.klein;
2
3 public class Animal {
4 private int id;
5
6 private String name;
7
8 public int getId() {
9 return id;
10 }
11
12 public void setId(int id) {
13 this.id = id;
14 }
15
16 public String getName() {
17 return name;
18 }
19
20 public void setName(String name) {
21 this.name = name;
22 }
3, 程序入口
1 package com.klein;
2
3 import org.hibernate.Session;
4 import org.hibernate.SessionFactory;
5 import org.hibernate.Transaction;
6 import org.hibernate.cfg.Configuration;
7 public class Client {
8
9 public static void main(String[] args) {
10
11 Configuration configuration = new Configuration().configure();
12 SessionFactory factory = configuration.buildSessionFactory();
13 Session session = factory.getCurrentSession();
14
15 Transaction transaction = session.beginTransaction();
16
17 Animal animal = new Animal();
18 animal.setName("Tiger");
19 session.save(animal);
20
21 transaction.commit();
22 }
23 }