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等。

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,449評論 0 10
  • 大學三年級了,出去做過家教,促銷員,播音,飯店服務員等兼職,在學校里也勤工助學;在學校里參加過舞蹈,小品...
    且如微風閱讀 187評論 0 0
  • 曾經在3-8歲小朋友群體中紅極一時的《歪歪兔和她的朋友們》第二季強勢回歸!據悉,這部國內首部原創手偶動畫劇正在緊鑼...
    孤獨癥康復閱讀 421評論 0 1
  • 所有云淡風輕的喜歡 都在今天之后明天之前不了了之 和黑色一樣消逝在黑夜中 其實有那些時候我在想 你說一句流浪天涯 ...
    故生閱讀 400評論 0 3
  • 說實話 我不過是一個偽文青 我喜歡文藝的生活 可是我卻很少購買裝飾去營造 不是因為我沒時間 其實是懶 所以 生活還...
    一杯野性的白開閱讀 545評論 0 1