1.2.5 The problem of data navigation(数据navigation问题)
在Java里与relational database里数据navigation有很根本的不同.在Java世界里,当我们要想访问一个User里的billing information时,我们可以通过下面的方式就可以了:
aUser.getBillingDetails().getAccountNumber();
不过,很不幸的是,这并不是从SQL里取数据的efficient方式.
为了提高数据库的访问性能,一个很重要的原则就是尽量地减少对数据库的访问次数,而这里最为明显的措施就是减少SQL的查询语句.(Of course, there are
other more sophisticated ways that follow as a second step.
这个other more sophisticated ways又在哪呢?)
于是,
用SQL访问数据库的高效方式就是用joins来连接所涉及的表.
表的数量多少也就决定了the depth of the object network you can navigate in memory. For example, if you need to retrieve a User and aren’t interested in the user’s billing information, you can write this simple query:
select * from USERS u where u.USER_ID = 123
On the other hand, if you need to retrieve a User and then subsequently visit each of the associated BillingDetails instances (let’s say, to list all the user’s credit cards), you write a different query:
select *
from USERS u
left outer join BILLING_DETAILS bd on bd.USER_ID = u.USER_ID
where u.USER_ID = 123
正如上面我们看到的那样,
为了高效地应用joins这个特性,我们事先得得知想访问的object network中的别的部分(如上面提到的BILLING_DETAILS).As you can see, to efficiently use joins you need to know what portion of the object network you plan to access when you retrieve the initial User—this is before you start navigating the object network!
然而,当一个对象首次accessed时,any object persistence solution提供了功能以获得与之关联对象的数据.不过,这个piecemeal style of data access is fundamentally inefficient in the context of a relational database,because it requires executing one statement for each node or collection of the object network that is accessed
(这句话说的什么呢?each node怎样?collecton of the object network又怎样?这个与executing one statement又怎样呢?). This is the dreaded n+1 selects problem.
(这里的n+1是什么意思?n+1是指n+1次访问BD,还是这个n指User下的n个BILLING_DETAILS?这样就很是浪费:一是数据库端多表join的浪费,二是那么多个BILLING_DETAILS信息传送时浪费,三是传来放到内存中的浪费.) 在Java世界里与Relational database世界里访问数据方式的不同或许是Java应用中性能问题的一个根本原因.There is a natural tension between too many selects and too big selects, which retrieve unnecessary information into memory. Yet, although we’ve been blessed with innumerable books and magazine articles advising us to use StringBuffer for string concatenation, it seems impossible to find any advice about strategies for avoiding the n+1 selects problem. Fortunately, Hibernate provides sophisticated features for efficiently and transparently fetching networks of objects from the database to the application accessing them. We discuss these features in chapters 13, 14, and 15.
1.2.6 The cost of the mismatch
We now have quite a list of object/relational mismatch problems, and it will be costly (in time and effort) to find solutions, as you may know from experience. This cost is often underestimated, and we think this is a major reason for many failed software projects. In our experience (regularly confirmed by developers we talk to), the main purpose of up to 30 percent of the Java application code written is to handle the tedious SQL/JDBC and manual bridging of the object/relational paradigm mismatch. Despite all this effort, the end result still doesn’t feel quite right. We’ve seen projects nearly sink due to the complexity and inflexibility of their database abstraction layers. We also see Java developers (and DBAs) quickly lose their confidence when design decisions about the persistence strategy for a project have to be made.
One of the major costs is in the area of modeling. The relational and domain models must both encompass the same business entities, but an object-oriented purist will model these entities in a different way than an experienced relational data modeler would. The usual solution to this problem is to bend and twist the domain model and the implemented classes
(是要把这个domain model及其实现类"打磨"至这些实现类与SQL的schema相符为止?那也就是说要把domain model往BD端靠了,这个靠也就意味着Hibernate里的自动生成BD schema没什么用了?那这个与前面的legacy问题时一段话岂不冲突?因为前面说的是可能会遇到一些legacy DB schema,这个意思说Hibernate会自动生成DB schema吗?有些乱了....) until they match the SQL database schema. (Which, following the principle of data independence, is certainly a safe long-term choice.)
This can be done successfully, but only at the cost of losing some of the advandages of object orientation(这名话的意思并不会最终bend and twist to SQL DB schema了?). Keep in mind that relational modeling is underpinned by relational theory. Object orientation has no such rigorous mathematical definition or body of theoretical work, SO WE CAN’T LOOK TO MATHEMATICS TO EXPLAIN HOW WE SHOULD BRIDGE THE GAP BETWEEN THE TWO PARADIGMS—THERE IS NO ELEGANT TRANSFORMATION WAITING TO BE DISCOVERED. (Doing away with Java and SQL, and starting from scratch isn’t considered elegant.)
The domain modeling mismatch isn’t the only source of the inflexibility and the lost productivity that lead to higher costs. A FURTHER CAUSE IS THE JDBC API ITSELF. JDBC and SQL provide a statement-oriented (that is, command-oriented) approach to moving data to and from an SQL database. If you want to query or manipulate data, the tables and columns involved must be specified at least three times
(怎么说至少要三次呢?) (insert, update, select), adding to the time required for design and implementation. The distinct dialects for every SQL database management system don’t improve the situation.
To round out your understanding of object persistence, and before we approach possible solutions, we need to discuss application architecture and the role of a persistence layer in typical application design.
分享到:
相关推荐
【标题】"java读书笔记笔记笔记笔记笔记笔记" 暗示了这是一份关于Java编程语言的学习笔记,可能包含了作者在阅读Java相关书籍时所做的重要记录和理解。笔记通常涵盖了语言的基础概念、核心特性、类与对象、内存管理...
总之,《深入浅出Hibernate》读书笔记涵盖了实体对象生命周期的转换、实体对象的识别机制以及Hibernate的多级缓存策略,这些都是理解并有效使用Hibernate的关键点。通过深入学习这些概念,开发者能够更好地掌握...
学生读书笔记共享-学生读书笔记共享系统-学生读书笔记共享系统源码-学生读书笔记共享管理系统-学生读书笔记共享管理系统java代码-学生读书笔记共享系统设计与实现-基于springboot的学生读书笔记共享系统-基于Web的...
学生读书笔记共享-学生读书笔记共享系统-学生读书笔记共享系统源码-学生读书笔记共享管理系统-学生读书笔记共享管理系统java代码-学生读书笔记共享系统设计与实现-基于springboot的学生读书笔记共享系统-基于Web的...
hibernate读书笔记,里面有一些实例和解说,是自己整理的,非常适合开发时做参考
Hibernate学习笔记整理 以下是 Hibernate 框架的详细知识点: Hibernate 介绍 Hibernate 是一个 ORM(Object-Relational Mapping)框架,用于将 Java 对象映射到数据库表中。它提供了一个简洁的方式来访问和操作...
java虚拟机读书笔记,主要描述阅读Java虚拟机核心技术的描述。
【Java相关课程系列笔记之十四Hibernate学习笔记】 Hibernate是一个开源的对象关系映射(ORM)框架,它极大地简化了Java应用程序对数据库的操作。本笔记将详细阐述Hibernate的核心概念、使用方法和特性。 一、...
通过阅读《Java游戏编程读书笔记》这本书,你可以深入了解这些概念并学习如何将它们应用到实际项目中。文档中的内容可能涵盖了Java游戏开发的基本原理、代码示例、最佳实践以及常见问题的解决方案。通过深入学习和...
读书笔记:《数据结构与算法分析 java语言描述》 读书笔记
读书笔记:「数据结构与算法分析Java语言描述」读书笔记。
### 马士兵Java框架Hibernate学习笔记 #### 一、HelloWorld - **知识点概述**: - **项目搭建**:介绍了如何从零开始搭建一个简单的Hibernate项目。 - **环境配置**:包括了如何配置MySQL数据库、创建必要的表...
计算机技术的读书笔记10篇计算机技术的读书笔记10篇计算机技术的读书笔记10篇计算机技术的读书笔记10篇计算机技术的读书笔记10篇