ORM
ORM
What is Object/Relational Mapping
http://hibernate.org/orm/what-is-an-orm/
Object Relational Mapping
对象关系映射
Object-relational mapping(ORM, O/RM, and O/R mapping tool) in computer science is a programming technique for converting data between incompatible type systems using object-oriented programming languages. This creates, in effect, a "virtual object database" that can
Many popular database products such as SQL database management systems(DBMS) can only store and manipulate scalar values such as integers and strings organized within tables. The programmer must either convert the object values into groups of simpler values for storage in the database(and convert them back upon retrieval), or only use simple scalar values within the program. Object-relational mapping implements the first approach.
The heart of the problem involves translating the logical representation of the objects into an atomized form that is capable of being stored in the database while preserving the properties of the objects and their relationships so that they can be reloaded as objects when needed. If this storage and retrieval functionality is implemented, the objects are said to be persistent.
与传统数据存取技术的对比
优点:
ORM 要写的代码更少
缺点:
ORM tools 的使用,使得我们不能在代码中使用复杂的实现代码。
Persistence
Persistence simply means that we would like our application's data to outlive the applications process. In Java terms, we would like the state of (some of) out objects to live beyond the scope of the JVM so that same state is available later.
- (在 Hibernate 中)ORM,是为了帮助应用程序获得持久性,也就是希望应用程序的数据能生存得比应用进程更久一点。
Relational Databases
Specifically, Hibernate ORM is concerned with data persistence as it applies to relational databases(RDBMS). In the world of Object-Oriented applications, there is often a discussion about using an object database(ODBMS) as opposed to a RDBMS. We are not going to explore that discussion here. Suffice it to say that RDBMS remain a very popular persistence mechanism and will so for the foreseeable future.
- 具体来说,(Hibernate) ORM 是关心关系型数据库中的数据持久性。
The Object-Relational Impedance Mismatch
‘Object-Relational Impedance Mismatch’(sometimes called the ‘paradigm mismatch’)is just a fancy way of saying that object models and relational models do not work very well together. RDBMSs represent data in a tabular format(a spreadsheet is a good visualization for those not familiar with RDBMSs), whereas object-oriented languages, such as Java, represent it as an interconnected graph of objects. Loading and storing graphs of objects using a tabular relational database exposes us to 5 mismatch problems...
- Object-Relational Impedance Mismatch 是指 object models 和 relational models 不能很好的配合工作。RDBMSs 用列的形式来表示数据,面向对象语言,比如 Java。从 RDBMSs 中读取或者存储 graphs of objects 的数据时,暴露给我们 5 个 mismatch problems
粒度
继承
继承是面向对象语言中一个常见的特性,然而,RDBMSs 并不定义任何类似的东西。
身份
RDBMS 通过 the primary key 来识别唯一性,java,则是来通过 a.equals(b) 来确定 a == b。
关联
关联在面向对象语言里是通过单向的引用,而在RDBMSs 里是通过foreign keys。如果在 Java 里要用到双向的关系,就必须定义 association 两次。
数据浏览
In Java,you navigate from one association to an other walking the object network.
You typically want to minimize the number of SQL queries and thus load several entities via JOINs and select the targeted entities before you start walking the object net work.
Persistence Layer, ORM(Object-Relation Mapping), DAO(Data Access Object) 的区别
持久层(Persistence Layer)是软件体系结构的中的一层,相对于业务层和展示层等。数据访问对象(Data Access Object)是面向对象软件中对访问RDBMS或文件系统的一个封装的模式,通常是将数据访问这部分的逻辑与业务逻辑分离,保证业务对象的代码不随持久化方式的变化而变化。这个模式只是很多持久层模式中的一种。ORM(Object-Relation Mapping),是一种软件技术,解决的问题是面向对象的软件和关系型数据库的不匹配的问题,将对象,对象的属性,以及对象之间的关系保存在关系型数据库的表中。通过配置建立对象和表的映射关系,而由系统自动生成插入,更新,删除和查询的SQL语句。这类技术包括Hibernate、iBatis等。