开始学习Hibernate

今天去Emule上搜了本hibernate的好书-Professional Hibernate。 准备仔细阅读一遍以掌握一门持久层的技术。这样我就可以用struts+Hibernate做出一个象样的web application出来。

今天晚上和我那个刚组建的魔兽战队的队员们玩了一天,所以,只有抽出晚上这一点时间来学习第1章,因为有了一定的基础,所以看起来也是比较快的。

第1章就是笼统的介绍了下将oop里面的object映射到关系数据库。一开篇,作者就介绍了现下比较流行的将object持久的技术,分别是:Serialization,XML和Object-oriented database systems mapping。并且分别分析了下这3种技术的优点和缺点。

1.先说下Serialization.这种技术是把object里面所有的attribute以及class types保存在一个文件或者缓冲器内。然后以一个string的形式保存到关系数据库的一个数据行中或者发送给其他应用程序。举个例子:

public class CD implements Serializable {
String title;
String artist;
public CD(String title, String artist) {
this.title = title;
this.artist = artist;
}
}

这是一个CD的类。这个class有两个attribute,分别是title和article,这两个attribute在object被序列化的时候都需要被保存。在java里,所有primitive类型和基类都实现了Serializable接口,系统会自动将其序列化。

对CD class的序列化将生成一个当前对象的数据的一个2进制描述。这个2进制的描述将被保存到一个关系数据库的BLOB字段中。这种处理就可以让其他的程序得到这个object的数据了。这些都是这种方法作为object持久化的好处,但是这种方法有他的不足:序列化的过程并不快,一个大的object将会花费很长时间把它转变成一个2进制的描述。所以,序列化都是在特殊的情况下才会被用到,作为数据的持久,还是要避免。

2.XML.

XML是当今非常流行的技术。举个例子:

<cd>
       <title>
            Grace Under Pressure
       </title>
       <artist>
             Rush
       </artist>
</cd>

一个数据库处理这个XML文件就可以创建这样一个schema:

create table cd (
title varchar,
artist varchar
);

通过这个xml文件我们也可以很轻松的创建一个class

public class cd {
String title;
String artist;
}

这段看不懂。原句:Having the XML representation creates an additional level of complexity and processing required to go
from an object to the database. This processing can be extensive, given the complexity of the objects in
an application.

3.Object-Oriented Database Systems

面向对象的数据库提供了对象存贮的透明性和方便性,但是查询起来并不方便

select x from user x where x.name = /"John Doe/"");这样一条查询语句,就是要查询所有的user对象,然后返回name属性为John Doe的。这种面向对象的数据库现在在市场上也并不怎么流行。

以上3中方式都有其不足,如果你正维护或者开发一个数据库的应用程序,现在最有可能用到的还是像oracle,Mysql,Microsoft SQL Server这种主流关系型数据库。而将object保存到一个关系型数据库中的过程就叫做Mapping(映射)。映射是一门将object里面的attribute保存在数据库表的一个或多个字段中。

所以上面的CD class就可以创建一个这样的数据表:

create table CD (
ID int not null primary key auto_increment,
title varchar(256),
artist varchar(256)
);

但是,你处理的class并不一定只包含简单的属性,比如

public class CD implements Serializable {
   String title;
   String artist;
   ArrayList tracks;
   public CD(String title, String artist){
           this.title = title;
           this.artist = artist;
           tracks = new ArrayListO;
   }
   public void addTrack(String track) {
           tracks. add(track);
   }
}

大家注意下tracks属性,他可以是null的也可以是成千上万的track。所以不可能在原来的表中给他一个或者多个固定的字段去保存它。对这个问题,提供的方法是给track建一另个表。

create table CD_tracks (
ID int not null primary key auto_increment,
cd_id int,
track varchar(256)
);

cd_id字段用来说明这个track是属于哪个cd的。

CD class可以再复杂一些:

public class CD implements Serializable {
String title;
String artist;
ArrayList tracks;
public CD(String title, String artist) {
this.title = title;
this.artist = artist;
tracks = new ArrayList();
}
public void addTrack(Track track) {
tracks.add(track);
}
private class Track {
String name;
int length;
public track(String name, int length) {
this.name = name;
this.length = length;
}
}
}这中情况依然是再建一个表。

解决oop中的继承

有这样三种方式:

Lowest-Child Inheritance Mapping:就是自继承的最下层将上追溯,将这个继承链上所有类的属性都作为一个字段,创建一个表。用来区分是哪个类的数据元是加入一个type属性来区分的。

Table-Per-Class Inheritance Mapping:不用多解释,顾名思义。

Table-Per-Concrete-Class Inheritance Mapping:就是save的是哪个类就持久哪个类。

这个会在以后详细介绍

处理关系(relationship)

One-to-One Relationship:设置一个外键。

One-to-Many Relationship:设置外键。

Many-to-Many Relationship:建一个中间表。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值