1. 开发环境准备
eclipse3.0.1
下载:http://www.eclipse.org/
eclipse plugin:hibernate synchronizer
下载:http://sourceforge.net/project/showfiles.php?group_id=99370
hibernate-2.1.8
下载:http://internap.dl.sourceforge.net/sourceforge/hibernate/hibernate-2.1.8.zip
mys ql 4.1
下载:http://www.mysql.com
2.演示一个简单例子
2.1在mysql数据库中的test数据库下创建表内容表content,脚本如下:
create table content (
fid int(11) not null auto_increment,
ftitle varchar(100) not null default ,
fcontent text ,
primary key (fid)
);
2.2检测hibernate插件是否安装成功,进入菜单”window”下的”preferences”,就有如下图所示的:
2.3用eclipse创建一个java项目,如下所示:
选“next>”一直到完成。
有一点要注意的是,创建完项目后,还要将hibernate库及mysql驱动加入到项目的libraries中。
2.4加入hibernate配置文件“hibernate.cfg.xml”
选择该项目,点右键,“new”à “other”进入如下界面,
选择“hibernate configuration file”à “next”
选择数据库类型、database url、username及password,ok, 文件“hibernate.cfg.xml”系统自动生成。
2.5加入“*.hbm”配置文件
同样是,选择该项目,点右键,“new”à “other”进入,不过这次要选择的是“hibernate mapping file”,点“next”进入如下界面
注意,如果数据库有密码,则要先要输入密码再点“refresh”按钮,数据中的表就会显示在tables框中。选择一个表,如content后,点“finish”按钮。
2.6根据content.hbm生成相关的java代码,选择content.hbm右键,操作如下界面:
2.7接一来简单地增加几行代码就可以了,进入系统生成的包dao中,如例子中的“com.hibernate.example.dao”,打开文件contentdao.java,代码修改如下:
0: /*
1: * 创建日期 2004-12-10
2: * @author oscar lao
3: * copyright 2005 s.w.s
4: */
5:
6: package com.hibernate.example.dao;
7:
8: import org.apache.log4j.logger;
9:
10: import com.hibernate.example.content;
11: import com.hibernate.example.base.basecontentdao;
12:
13: /**
14: * @author oscar lao
15: * qq:63065068
16: * email:e-silver@163.com
17: * classdesc:hibernate example
18: **/
19: public class contentdao extends basecontentdao {
20: private static final logger logger = logger.getlogger(contentdao.class);
21:
22: public static void main(string[] args){
23: if (logger.isdebugenabled()) {
24: logger.debug("程序正在运行…");
25: }
26:
27: try {
28: _rootdao.initialize();
29:
30: contentdao cond = new contentdao();
31: content cont = new content();
32: for (int i = 1; i < 100; i++) {
33: cont.setfid(new integer(i));
34: cont.setftitle("title"+i);
35: cont.setfcontent("content"+i);
36: cond.save(cont);
37: }
38: } catch (exception e) {
39: logger.error("运行时出现异常…", e);
40: }
41:
42: if (logger.isdebugenabled()) {
43: logger.debug("运行完毕。");
44: }
45: }
46: }
3.欣赏成果
如果所有的配置正确,contentdao类应该可以运行了,运行后,程序会向表content插入100条记录。祝你好运!!
4.在我测试时出现如下错误,现在也贴出来,希望对大家有所帮助:
4.1如果运行时出现如下异常
could not find usertransaction in jndi javax.naming.noinitialcontextexception:…
解决:把配置文件“hibernate.cfg.xml”中的
<property name="hibernate.transaction.factory_class">
net.sf.hibernate.transaction.jtatransactionfactory
</property>
<property name="jta.usertransaction">
java:comp/usertransaction
</property>
两行设置jta控制事务的属性注释掉就可以了,因为程序里还未使用jta控制事务。
4.2 如果运行时出现如下异常
error parsing xml: /hibernate.cfg.xml(33) the content of element type "session-factory" is incomplete, it must match "(property*,mapping+,(class-cache|collection-cache|jcs-class-cache|jcs-collection-cache)*)".
解决:在配置文件“hibernate.cfg.xml”中的<session-factory>与</session-factory>之间加
<mapping resource="content.hbm" />
第一次写关于hibernate的文章,如有不对的地方,请批评指正。
最后,祝大家成功!!^_^
