2005-8-20 09:13下午
hibernate 显式使用trasaction通常用于跨越多个http get请求的事务要求时使用,否则可以直接使用隐式的事务模式
hibernate开发步骤
1.撰写所有的pojo与数据库对应
2.撰写与pojo对应的hbm.xml
3.撰写业务操作类,操作pojo
4.撰写配置hibernate.cfg.xml
5.撰写build.xml自动部署
5.发布:hibernate3.jar
为hibernate程序提供运行参数的方式:
1.
Configuration cfg = new Configuration()
.addClass(org.hibernate.auction.Item.class)
.addClass(org.hibernate.auction.Bid.class)
.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect")
.setProperty("hibernate.connection.datasource", "java:comp/env/jdbc/test")
.setProperty("hibernate.order_updates", "true");
向configuration传递java property
2.撰写hibernate.properties文件在classpath的根目录
3.使用java -Dname=value提供参数
4.在hibernate.cfg.xml文件中使用property节点
开发过程中曾出现的问题:
*.cfg.xml中的class节点写成"UserInfo",应该是"studyHibernate.UserInfo"
造成"cound not load mappings from userinfo.hbm.xml"异常"
*.build.xml中的class节点写成"UserInfoManager",应该是"studyHibernate.UserInfoManager"
*.将hbm.xml和cfg.xml放在的相应的包目录中应该将其放在src根目录下
,否则造成"NoDefClassFound"异常.
*.build.xml应放在项目目录下
*.将db2java.zip拷贝到lib目录后,运行有错,说不能找到db2的jdbc驱动,原因是容器并不解压zip文件,将该文件名改为db2java.jar后运行正常
db2的jdbc连接有4种方式,ibm网站有一篇文章详述,我使用的如下的方式,
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">COM.ibm.db2.jdbc.net.DB2Driver</property>
12
<property name="connection.url">jdbc:db2://localhost:50000/lw2005</property>
13
<property name="connection.username">db2admin</property>
14
<property name="connection.password">Db2admiN</property>
15
16
<!-- JDBC connection pool (use the built-in) -->
17
<property name="connection.pool_size">1</property>
18
19
<!-- SQL dialect -->
20
<property name="dialect">org.hibernate.dialect.DB2Dialect</property>
21
22
<!-- Echo all executed SQL to stdout -->
23
<property name="show_sql">true</property>
24
<property name="format_sql">true</property>
25
26
<!-- Drop and re-create the database schema on startup -->
27
<property name="hbm2ddl.auto">create</property>
28
29
<mapping resource="userinfo.hbm.xml"/>
30
31
</session-factory>
32
33
</hibernate-configuration>