Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » 技术文章库  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Hibernate技术
wzy4322cn



发贴: 0
积分: 0
于 2004-01-29 11:33 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
一、前言

刚刚接触java的数据库层次的技术的时候,使用的是JDBC,后来做的项目越来越大了,觉得JDBC到处建立连接,或者使用直接的sql语句,修改或者项目变更起来很不方便,于是打算自己写一个基于JDBC的上一层的程序,所有的访问都通过这个程序转换为JDBC的代码来访问数据库。

client

DBAccess

JDBC

db

client















这种技术的采用确实给开发工作带来了十分的改观,所有的对数据库的操作变成了对某个对象的操作,我采用了java语言特有的reflection机制,动态的取得某个Javabean的属性,然后对应到(mapping)某个数据库的表中,这样,客户端的代码就变得非常简单,例如:



DBAccess dba = new DBAccess();

Foo f = new Foo();

f.setBar(aBar);

…//Setting properties for class Foo

dba.insertRose;



只要简单的几步操作就可以完成插入数据库的操作。其实我在这里假定了一些设置,比如大部分的java类型比如int,long,double,String我都mapping成为数据库的String类型,而且为了知道某个javabean对应的table名称,以及主键的名字,我特地在javabean里面添加了两个属性来记录这些信息。由于没有采用xml技术来配置这些信息,使得我的项目管理十分麻烦。



Hibernate,它实际上是一个o/r mapping工具,把java class对应到数据库的table中,采用了xml技术,java reflection技术等,基本的原理我觉得和我上面介绍的类还是比较的类似的。











浅析另类O/R Mapping工具----hibernate



之所以说hibernate是一个比较另类的东西,是由于目前业界争吵不休的JDO技术,JDO似乎正在成为一个标准,JBOSS也出了一个什么DO,大家都在争取成为事实上的标准,但是在世界的另异域,有一些程序高手默默的维护着、发展着hibernate技术。Hibernate技术本质上也是遵守的ODMG标准的,它的出色源于不断的升级、不断的维护、修改。以及完善的文档、人气很旺的论坛。还有很重要的一点就是它的易学易用性。

这里我就举一个比较浅显的例子,相当于我们程序员都知道的HELLOWORLD程序。但是这第一步也是非常重要的。





二、下载并且安装hibernate

hibernate是sourceforge的一个子项目,从google上面搜索hibernate你可以搜索到hibernate的所有连接。具体的,你可以从http://hibernate.sourceforge.net上面获得下载的连接,目前最新的release版本是hibernate 2.0.1(2003-6-17),当然,你也可以从cvs上面获得源代码,然后自行编译。



三、Hibernate基本概念



Hibernate技术本质上是一个提供数据库服务的中间件。它的架构如下图所示:



此图显示了hibernate的工作原理,它是利用数据库以及其他一些配置文件如hibernate.properties,XML Mapping等来为应用程序提供数据持久服务的。它的API中包含以下一些主要的类。



1、SessionFactory (net.sf.hibernate.SessionFactory)

包含已经编译的映射(mappings),是制造session的工厂,可能含有一些可以在各个事务(transaction)之间共享的数据。

2、Session (net.sf.hibernate.Session)

单线程的,短寿命的对象,代表了一次会话的过程。实际上是把一个JDBC Connection打包了,它可以包含一些持久化对象的缓存。

3、Persistent Objects and Collections

单线程的,短寿命的对象,包含了各种属性字段以及一些商业逻辑,它们可能仅仅是一些普通的javabeans,但是它必然在某个时刻只能和一个session相关联。



我就介绍这些吧,详细的大家可以看hibernate自带的文档,里面写的非常详细。



四、开始写程序吧

我准备用一个比较好的例子来简要的说明hibernate的强大功能。我的这个程序使用了其他一些相关的技术如ANT,LOG4J,JUIT等等,如果大家希望得到这些技术相关文档,可以和我联系。

在开始这个例子之前,我详细介绍一下这个例子程序的大概情况,我将循序渐进的介绍hibernate的功能,为此,我将先写一个简单的例子程序,然后我们再把它做复杂。

是这样的,我们假设:我们要写一个关于儿童(Child)的应用程序,主要管理儿童的相关信息(如儿童玩具Toy),那么我们首先要建模。如下:

实际类的代码:

Child类,主要含有一个id,和一个toys属性,表示一个Child可以拥有多个toys,采用的是数组形式。

public class Child {

public int getId(){

return id;

}

public void setId(int id){

this.id = id;

}

public void setToys(Toy[] toys) {

this.toys = toys;

}

public Toy[] getToys() {

return toys;

}



private int id;

private Toy[] toys;

}

类Toy

public class Toy {

public int getToyId(){

return toyId;

}

public void setToyId(int toyId){

this.toyId = toyId;

}

public String getName(){ return name; }

public void setName(String name){ this.name = name; }

private int toyId;

private String name;

}

我们可以看见,一个儿童可以具有很多玩具,这是一个明显的一对多关系,在Hibernate里面有这样的标签与之对应<one-to-many>,然后,我们可以为这两个类写mapping.xml文件了,在hibernate里面,每个要持久化的类都要求有相应的mapping文件,这样hibernate才会知道如何把java class property对应到table columns里面了,有的时候我们不用自己指明java某个属性的类型,hibernate有reflection机制,它可以在运行时动态的察觉某个属性的类型,然后对应到相应的数据库类型中。



作者 Re:Hibernate技术 [Re:wzy4322cn]
yekai





发贴: 256
积分: 50
于 2004-01-31 20:34 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
好文,终于对Hibernate有了一些了解。


作者 Re:Hibernate技术 [Re:wzy4322cn]
yatwql





发贴: 33
积分: 0
于 2004-02-16 22:11 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
You can go to www.hibernate.org or forum.hibernate.org.cn( Forum in Chinese ) for more information


作者 Re:Hibernate技术 [Re:wzy4322cn]
Lomone



发贴: 0
积分: 0
于 2004-03-09 21:31 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
good article
I know some about Hibernate now



作者 Re:Hibernate技术 [Re:wzy4322cn]
bwpc





发贴: 95
积分: 2
于 2004-04-01 16:46 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
good,
go on



作者 Re:Hibernate技术 [Re:wzy4322cn]
feaket





发贴: 2
积分: 0
于 2004-05-10 13:47 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
good

www.hibernate.org.cn



作者 Re:Hibernate技术 [Re:wzy4322cn]
cqzym1



发贴: 0
积分: 0
于 2004-05-19 23:53 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
还不错


作者 Re:Hibernate技术 [Re:wzy4322cn]
小开ye





发贴: 4
积分: 0
于 2004-12-08 18:03 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
留名



flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923