一个简单的 hibernate 配置程序
第一步:准备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/HibernateConfigurationDTD3.0//EN"
4"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5
6<hibernate-configuration>
7
8<session-factory>
9
10<!--Databaseconnectionsettings-->
11<propertyname="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
12<propertyname="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property>
13<propertyname="connection.username">scott</property>
14<propertyname="connection.password">tiger</property>
15
16<!--SQLdialect-->
17<propertyname="dialect">org.hibernate.dialect.Oracle9Dialect</property>
18
19<!--EnableHibernate'sautomaticsessioncontextmanagement-->
20<propertyname="current_session_context_class">thread</property>
21
22<!--EchoallexecutedSQLtostdout-->
23<propertyname="show_sql">true</property>
24
25<!--Dropandre-createthedatabaseschemaonstartup-->
26<propertyname="hbm2ddl.auto">create</property>
27
28<mappingresource="com/klein/Animal.hbm.xml"/>
29
30</session-factory>
31
32 </hibernate-configuration>复制代码
log4j.properties
1 ### direct log messages to stdout ###
2log4j.appender.stdout=org.apache.log4j.ConsoleAppender
3log4j.appender.stdout.Target=System.out
4log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
5log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE}%5p%c{1}:%L-%m%n
6
7###directmessagestofilehibernate.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###setloglevels-formoreverboseloggingchange'info'to'debug'###
14
15log4j.rootLogger=warn,stdout
16
17log4j.logger.org.hibernate=info
18#log4j.logger.org.hibernate=debug
19
20###logHQLqueryparseractivity
21#log4j.logger.org.hibernate.hql.ast.AST=debug
22
23###logjusttheSQL
24#log4j.logger.org.hibernate.SQL=debug
25
26###logJDBCbindparameters###
27log4j.logger.org.hibernate.type=info
28#log4j.logger.org.hibernate.type=debug
29
30###logschemaexport/update###
31log4j.logger.org.hibernate.tool.hbm2ddl=debug
32
33###logHQLparsetrees
34#log4j.logger.org.hibernate.hql=debug
35
36###logcacheactivity###
37#log4j.logger.org.hibernate.cache=debug
38
39###logtransactionactivity
40#log4j.logger.org.hibernate.transaction=debug
41
42###logJDBCresourceacquisition
43#log4j.logger.org.hibernate.jdbc=debug
44
45###enablethefollowinglineifyouwanttotrackdownconnection###
46###leakageswhenusingDriverManagerConnectionProvider###
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/HibernateMappingDTD3.0//EN"
4"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
5
6<hibernate-mapping>
7<classname="com.klein.Animal"table="t_animal">
8<idname="id">
9<generatorclass="native"/>
10</id>
11<propertyname="name"/>
12</class>
13 </hibernate-mapping>复制代码
表对象Animal
1 package com.klein;
2
3publicclassAnimal{
4privateintid;
5
6privateStringname;
7
8publicintgetId(){
9returnid;
10}
11
12publicvoidsetId(intid){
13this.id=id;
14}
15
16publicStringgetName(){
17returnname;
18}
19
20publicvoidsetName(Stringname){
21this.name=name;
22 }复制代码
3,程序入口
1 package com.klein;
2
3importorg.hibernate.Session;
4importorg.hibernate.SessionFactory;
5importorg.hibernate.Transaction;
6importorg.hibernate.cfg.Configuration;
7publicclassClient{
8
9publicstaticvoidmain(String[]args){
10
11Configurationconfiguration=newConfiguration().configure();
12SessionFactoryfactory=configuration.buildSessionFactory();
13Sessionsession=factory.getCurrentSession();
14
15Transactiontransaction=session.beginTransaction();
16
17Animalanimal=newAnimal();
18animal.setName("Tiger");
19session.save(animal);
20
21transaction.commit();
22}
23 }复制代码