- 浏览: 922446 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (322)
- Hibernate研究&源码 (27)
- Server (10)
- Coder碎语 (64)
- EnglishMulling (11)
- About XML (1)
- persistence (12)
- Core Java & OO (23)
- Java EE (6)
- JavaScript/JSON/Ajax/ext... (22)
- 我的读书笔记 (16)
- Source Codes Study (29)
- workFlow/jBPM (22)
- OFBiz: Open For Business (1)
- 项目积累 (21)
- srcStudy_acegi (1)
- Cache/Ehcache... (9)
- Java Test/JUnit.. (7)
- maven/ant (2)
- 设计模式 (1)
- SOA/cxf/ws-security (2)
- Android (4)
- 云计算/Hadoop (2)
- 加密/签名 (1)
- 正则表达式 (1)
- htmlparser (1)
- 操作系统 (5)
- DB (1)
最新评论
-
天使建站:
这里这篇文章更详细 还有完整的实例演示:js跳出循环 ...
jQuery中each的break和continue -
heshifk:
刚刚我也遇到同样的问题,然后就在纠结为什么不能直接使用brea ...
jQuery中each的break和continue -
masuweng:
不错写的.
集万千宠爱于一身的SessionImpl:get研究(四): Hibernate源码研究碎得(8) -
muzi131313:
这个老是忘,做一下笔记还是挺好的
jQuery中each的break和continue -
lg068:
data = data.replace("\n&qu ...
项目小经验: eval与回车符
1.3 Persistence layers and alternatives
In a medium- or large-sized application, it usually makes sense to organize classes by concern(这个by concern写的不错呀!). Persistence is one concern; others include presentation, workflow,and business logic. A typical object-oriented architecture includes layers of code that represent the concerns. It’s normal and certainly best practice to group all classes and components responsible for persistence into a separate persistence layer in a layered system architecture.
In this section, we first look at the layers of this type of architecture and why we use them. After that, we focus on the layer we’re most interested in—the persistence layer—and some of the ways it can be implemented.
1.3.1 Layered architecture
A layered architecture defines interfaces between code that implements the various concerns, allowing changes to be made to the way one concern is implemented without significant disruption to code in the other layers. Layering also determines the kinds of interlayer dependencies that occur. The rules are as follows:
■ Layers communicate from top to bottom. A layer is dependent only on the layer directly below it.
■ Each layer is unaware of any other layers except for the layer just below it.
Different systems group concerns differently, so they define different layers. A typical, proven, high-level application architecture uses three layers: one each for presentation, business logic, and persistence, as shown in figure 1.4.
Let’s take a closer look at the layers and elements in the diagram(见附件):
■ Presentation layer—The user interface logic is topmost. Code responsible for the presentation and control of page and screen navigation is in the presentation layer.
■ Business layer—The exact form of the next layer varies widely between applications. It’s generally agreed, however, that the business layer is responsible for implementing any business rules or system requirements that would be understood by users as part of the problem domain. This layer usually includes some kind of controlling component—code that knows when to invoke which business rule. In some systems, this layer has its own internal representation of the business domain entities, and in others it reuses the model defined by the persistence layer. We revisit this issue in chapter 3.
■ Persistence layer—The persistence layer is a group of classes and components responsible for storing data to, and retrieving it from, one or more data stores. This layer necessarily includes a model of the business domain entities (even if it’s only a metadata model).
■ Database—The database exists outside the Java application itself. It’s the actual, persistent representation of the system state. If an SQL database is used, the database includes the relational schema and possibly stored procedures.
■ Helper and utility classes—Every application has a set of infrastructural helper or utility classes that are used in every layer of the application (such as Exception classes for error handling). These infrastructural elements don’t form a layer, because they don’t obey the rules for interlayer dependency in a layered architecture. (这个就是那个界定layer的rules了)
Let’s now take a brief look at the various ways the persistence layer can be implemented by Java applications. Don’t worry—we’ll get to ORM and Hibernate soon.There is much to be learned by looking at other approaches.
1.3.2 Hand-coding a persistence layer with SQL/JDBC
The most common approach to Java persistence is for application programmers to work directly with SQL and JDBC. After all, developers are familiar with relational database management systems, they understand SQL, and they know how to work with tables and foreign keys. Moreover, they can always use the well-known and widely used data access object (DAO) pattern to hide complex JDBC code and nonportable SQL from the business logic.
The DAO pattern is a good one—so good that we often recommend its use even with ORM(就是应用了ORM后,DAO也很是有用). However, the work involved in manually coding persistence for each domain class is considerable, particularly when multiple SQL dialects are supported. This work usually ends up consuming a large portion of the development effort. Furthermore, when requirements change, a hand-coded solution always requires more attention and maintenance effort.
Why not implement a simple mapping framework to fit the specific requirements of your project? The result of such an effort could even be reused (这个reused就成了Hibernate的萌芽!)in future projects. Many developers have taken this approach; numerous homegrown object/relational persistence layers are in production systems today. However, we don’t recommend this approach. Excellent solutions already exist: not only the (mostly expensive) tools sold by commercial vendors, but also open source projects with free licenses. We’re certain you’ll be able to find a solution that meets your requirements, both business and technical. It’s likely that such a solution will do a great deal more, and do it better, than a solution you could build in a limited time.
Developing a reasonably full-featured ORM may take many developers months. For example, Hibernate is about 80,000 lines of code, some of which is much more difficult than typical application code, along with 25,000 lines of unit test code. This may be more code than is in your application. A great many details can easily be overlooked in such a large project—as both the authors know from experience! Even if an existing tool doesn’t fully implement two or three of your more exotic requirements, it’s still probably not worth creating your own tool. Any ORM software will handle the tedious common cases—the ones that kill productivity. It’s OK if you need to hand-code certain special cases; few applications are composed primarily of special cases.
1.3.3 Using serialization
Java has a built-in persistence mechanism: Serialization provides the ability to write a snapshot of a network of objects (the state of the application) to a byte stream, which may then be persisted to a file or database(把这样的byte stream再放进DB中又会有什么益处?为什么不直接把相应数据放进DB呢?). Serialization is also used by Java’s Remote Method Invocation (RMI) to achieve pass-by value semantics for complex objects(这个也一直没接触过,有机会试试). Another use of serialization is to replicate application state across nodes in a cluster of machines.
Why not use serialization for the persistence layer? Unfortunately, a serialized network of interconnected objects can only be accessed as a whole; it’s impossible to retrieve any data from the stream without deserializing the entire stream. Thus, the resulting byte stream must be considered unsuitable for arbitrary search or aggregation of large datasets. It isn’t even possible to access or update a single object or subset of objects independently. Loading and overwriting an entire object network in each transaction is no option for systems designed to support high concurrency.
Given current technology, serialization is inadequate as a persistence mechanism for high concurrency web and enterprise applications. It has a particular niche as a suitable persistence mechanism for desktop applications.
1.3.4 Object-oriented database systems
Because we work with objects in Java, it would be ideal if there were a way to store those objects in a database without having to bend and twist the object model at all. In the mid-1990s, object-oriented database systems gained attention. They’re based on a network data model, which was common before the advent of the relational data model decades ago. The basic idea is to store a network of objects, with all its pointers and nodes, and to re-create the same in-memory graph later on.This can be optimized with various metadata and configuration settings.
An object-oriented database management system (OODBMS) is more like an extension to the application environment than an external data store. An OODBMS usually features a multitiered implementation, with the backend data store, object cache, and client application coupled tightly together and interacting via a proprietary network protocol. Object nodes are kept on pages of memory, which are transported from and to the data store.
Object-oriented database development begins with the top-down definition of host language bindings that add persistence capabilities to the programming language. Hence, object databases offer seamless integration into the object-oriented application environment. This is different from the model used by today’s relational databases, where interaction with the database occurs via an intermediate language (SQL) and data independence from a particular application is the major concern.
For background information on object-oriented databases, we recommend the respective chapter in An Introduction to Database Systems (Date, 2003).
We won’t bother looking too closely into why object-oriented database technol ogy hasn’t been more popular; we’ll observe that object databases haven’t been
widely adopted and that it doesn’t appear likely that they will be in the near future. We’re confident that the overwhelming majority of developers will have far more opportunity to work with relational technology, given the current politi cal realities (predefined deployment environments) and the common require ment for data independence.
1.3.5 Other options
Of course, there are other kinds of persistence layers. XML persistence is a varia tion on the serialization theme; this approach addresses some of the limitations of byte-stream serialization by allowing easy access to the data through a stan dardized tool interface. However, managing data in XML would expose you to an object/hierarchical mismatch. Furthermore, there is no additional benefit from the XML itself, because it’s just another text file format and has no inherent capabilities for data management. You can use stored procedures (even writing them in Java, sometimes) and move the problem into the database tier. So-called object-relational databases have been marketed as a solution, but they offer only a more sophisticated datatype system providing only half the solution to our problems (and further muddling terminology). We’re sure there are plenty of other examples, but none of them are likely to become popular in the immedi ate future.
Political and economic constraints (long-term investments in SQL databases), data independence, and the requirement for access to valuable legacy data call for a different approach. ORM may be the most practical solution to our problems.
In a medium- or large-sized application, it usually makes sense to organize classes by concern(这个by concern写的不错呀!). Persistence is one concern; others include presentation, workflow,and business logic. A typical object-oriented architecture includes layers of code that represent the concerns. It’s normal and certainly best practice to group all classes and components responsible for persistence into a separate persistence layer in a layered system architecture.
In this section, we first look at the layers of this type of architecture and why we use them. After that, we focus on the layer we’re most interested in—the persistence layer—and some of the ways it can be implemented.
1.3.1 Layered architecture
A layered architecture defines interfaces between code that implements the various concerns, allowing changes to be made to the way one concern is implemented without significant disruption to code in the other layers. Layering also determines the kinds of interlayer dependencies that occur. The rules are as follows:
■ Layers communicate from top to bottom. A layer is dependent only on the layer directly below it.
■ Each layer is unaware of any other layers except for the layer just below it.
Different systems group concerns differently, so they define different layers. A typical, proven, high-level application architecture uses three layers: one each for presentation, business logic, and persistence, as shown in figure 1.4.
Let’s take a closer look at the layers and elements in the diagram(见附件):
■ Presentation layer—The user interface logic is topmost. Code responsible for the presentation and control of page and screen navigation is in the presentation layer.
■ Business layer—The exact form of the next layer varies widely between applications. It’s generally agreed, however, that the business layer is responsible for implementing any business rules or system requirements that would be understood by users as part of the problem domain. This layer usually includes some kind of controlling component—code that knows when to invoke which business rule. In some systems, this layer has its own internal representation of the business domain entities, and in others it reuses the model defined by the persistence layer. We revisit this issue in chapter 3.
■ Persistence layer—The persistence layer is a group of classes and components responsible for storing data to, and retrieving it from, one or more data stores. This layer necessarily includes a model of the business domain entities (even if it’s only a metadata model).
■ Database—The database exists outside the Java application itself. It’s the actual, persistent representation of the system state. If an SQL database is used, the database includes the relational schema and possibly stored procedures.
■ Helper and utility classes—Every application has a set of infrastructural helper or utility classes that are used in every layer of the application (such as Exception classes for error handling). These infrastructural elements don’t form a layer, because they don’t obey the rules for interlayer dependency in a layered architecture. (这个就是那个界定layer的rules了)
Let’s now take a brief look at the various ways the persistence layer can be implemented by Java applications. Don’t worry—we’ll get to ORM and Hibernate soon.There is much to be learned by looking at other approaches.
1.3.2 Hand-coding a persistence layer with SQL/JDBC
The most common approach to Java persistence is for application programmers to work directly with SQL and JDBC. After all, developers are familiar with relational database management systems, they understand SQL, and they know how to work with tables and foreign keys. Moreover, they can always use the well-known and widely used data access object (DAO) pattern to hide complex JDBC code and nonportable SQL from the business logic.
The DAO pattern is a good one—so good that we often recommend its use even with ORM(就是应用了ORM后,DAO也很是有用). However, the work involved in manually coding persistence for each domain class is considerable, particularly when multiple SQL dialects are supported. This work usually ends up consuming a large portion of the development effort. Furthermore, when requirements change, a hand-coded solution always requires more attention and maintenance effort.
Why not implement a simple mapping framework to fit the specific requirements of your project? The result of such an effort could even be reused (这个reused就成了Hibernate的萌芽!)in future projects. Many developers have taken this approach; numerous homegrown object/relational persistence layers are in production systems today. However, we don’t recommend this approach. Excellent solutions already exist: not only the (mostly expensive) tools sold by commercial vendors, but also open source projects with free licenses. We’re certain you’ll be able to find a solution that meets your requirements, both business and technical. It’s likely that such a solution will do a great deal more, and do it better, than a solution you could build in a limited time.
Developing a reasonably full-featured ORM may take many developers months. For example, Hibernate is about 80,000 lines of code, some of which is much more difficult than typical application code, along with 25,000 lines of unit test code. This may be more code than is in your application. A great many details can easily be overlooked in such a large project—as both the authors know from experience! Even if an existing tool doesn’t fully implement two or three of your more exotic requirements, it’s still probably not worth creating your own tool. Any ORM software will handle the tedious common cases—the ones that kill productivity. It’s OK if you need to hand-code certain special cases; few applications are composed primarily of special cases.
1.3.3 Using serialization
Java has a built-in persistence mechanism: Serialization provides the ability to write a snapshot of a network of objects (the state of the application) to a byte stream, which may then be persisted to a file or database(把这样的byte stream再放进DB中又会有什么益处?为什么不直接把相应数据放进DB呢?). Serialization is also used by Java’s Remote Method Invocation (RMI) to achieve pass-by value semantics for complex objects(这个也一直没接触过,有机会试试). Another use of serialization is to replicate application state across nodes in a cluster of machines.
Why not use serialization for the persistence layer? Unfortunately, a serialized network of interconnected objects can only be accessed as a whole; it’s impossible to retrieve any data from the stream without deserializing the entire stream. Thus, the resulting byte stream must be considered unsuitable for arbitrary search or aggregation of large datasets. It isn’t even possible to access or update a single object or subset of objects independently. Loading and overwriting an entire object network in each transaction is no option for systems designed to support high concurrency.
Given current technology, serialization is inadequate as a persistence mechanism for high concurrency web and enterprise applications. It has a particular niche as a suitable persistence mechanism for desktop applications.
1.3.4 Object-oriented database systems
Because we work with objects in Java, it would be ideal if there were a way to store those objects in a database without having to bend and twist the object model at all. In the mid-1990s, object-oriented database systems gained attention. They’re based on a network data model, which was common before the advent of the relational data model decades ago. The basic idea is to store a network of objects, with all its pointers and nodes, and to re-create the same in-memory graph later on.This can be optimized with various metadata and configuration settings.
An object-oriented database management system (OODBMS) is more like an extension to the application environment than an external data store. An OODBMS usually features a multitiered implementation, with the backend data store, object cache, and client application coupled tightly together and interacting via a proprietary network protocol. Object nodes are kept on pages of memory, which are transported from and to the data store.
Object-oriented database development begins with the top-down definition of host language bindings that add persistence capabilities to the programming language. Hence, object databases offer seamless integration into the object-oriented application environment. This is different from the model used by today’s relational databases, where interaction with the database occurs via an intermediate language (SQL) and data independence from a particular application is the major concern.
For background information on object-oriented databases, we recommend the respective chapter in An Introduction to Database Systems (Date, 2003).
We won’t bother looking too closely into why object-oriented database technol ogy hasn’t been more popular; we’ll observe that object databases haven’t been
widely adopted and that it doesn’t appear likely that they will be in the near future. We’re confident that the overwhelming majority of developers will have far more opportunity to work with relational technology, given the current politi cal realities (predefined deployment environments) and the common require ment for data independence.
1.3.5 Other options
Of course, there are other kinds of persistence layers. XML persistence is a varia tion on the serialization theme; this approach addresses some of the limitations of byte-stream serialization by allowing easy access to the data through a stan dardized tool interface. However, managing data in XML would expose you to an object/hierarchical mismatch. Furthermore, there is no additional benefit from the XML itself, because it’s just another text file format and has no inherent capabilities for data management. You can use stored procedures (even writing them in Java, sometimes) and move the problem into the database tier. So-called object-relational databases have been marketed as a solution, but they offer only a more sophisticated datatype system providing only half the solution to our problems (and further muddling terminology). We’re sure there are plenty of other examples, but none of them are likely to become popular in the immedi ate future.
Political and economic constraints (long-term investments in SQL databases), data independence, and the requirement for access to valuable legacy data call for a different approach. ORM may be the most practical solution to our problems.
发表评论
-
<Java.JavaEE面试整理>(12) 对Collections FrameWork的理解(二)
2008-04-06 15:27 2865<Java.JavaEE面试整理>(12) 对Co ... -
<Java.JavaEE面试整理>(11) 对Collections FrameWork的理解(一)
2008-04-06 13:55 2402Q 16:谈谈你对Java Collectio ... -
写博一月后的收获与反思(1)
2008-04-05 17:26 1649写博一月后的收获与反思开始安下心来写博到现在,细想一下也有一个 ... -
<Java.JavaEE面试整理>(9)--抽象类与接口有什么区别?以及如何选择?
2008-04-03 15:17 1903"abstract class" or & ... -
<Java.JavaEE面试整理>(8) --基于什么考虑,Java 1.4中引入Assert???
2008-04-03 11:44 1412Q 11: What is design by contrac ... -
JavaPersistenceWithHibernate读书笔记(4)
2008-04-03 10:41 21341.2.4 Problems relating to asso ... -
JavaPersistenceWithHibernate读书笔记(3)
2008-04-02 17:25 17521.2.2 The problem of subtypes ... -
<Java.JavaEE面试整理>(7) --Polymorphism之深入理解(二)
2008-04-02 14:31 1497Q. Why would you prefer code re ... -
<Java.JavaEE面试整理>(6) --Polymorphism之深入理解(一)
2008-04-02 12:14 2061Q 10:请谈谈你对多态,继承,封装和动态绑定(dynamic ... -
<Java.JavaEE面试整理>(5)
2008-04-01 08:49 1896Q 09: 请谈谈你对'is a'和'has a'的理解?也就 ... -
JavaPersistenceWithHibernate读书笔记(2)
2008-03-31 19:24 1181JavaPersistenceWithHibernate读书笔 ... -
<Java.JavaEE面试整理>(4)
2008-03-31 18:54 1434Q 06:Java中构造方法与其它常规方法有什么区别?要是你没 ... -
<Java.JavaEE面试整理>(3)
2008-03-30 16:31 1858Q 04: 怎么用Java里的Pack ... -
JavaPersistenceWithHibernate读书笔记(1)
2008-03-30 14:14 1778Part 1: Getting started with Hi ... -
<Java.JavaEE面试整理>(2)
2008-03-29 15:14 1816Java.J2EE.Job.Interview.Compani ...
相关推荐
学生读书笔记共享-学生读书笔记共享系统-学生读书笔记共享系统源码-学生读书笔记共享管理系统-学生读书笔记共享管理系统java代码-学生读书笔记共享系统设计与实现-基于springboot的学生读书笔记共享系统-基于Web的...
学生读书笔记共享-学生读书笔记共享系统-学生读书笔记共享系统源码-学生读书笔记共享管理系统-学生读书笔记共享管理系统java代码-学生读书笔记共享系统设计与实现-基于springboot的学生读书笔记共享系统-基于Web的...
学生读书笔记共享-学生读书笔记共享系统的设计与实现代码-java-springboot-基于springboot的学生读书笔记共享系统项目-代码-源码-项目-系统-毕设-网站 1、技术栈:java,springboot,vue,ajax,maven,mysql,...
《技术人的管理之路》读书笔记 --思维导图 《技术人的管理之路》读书笔记 --思维导图 《技术人的管理之路》读书笔记 --思维导图 《技术人的管理之路》读书笔记 --思维导图 《技术人的管理之路》读书笔记 --思维导图 ...
【标题】"java读书笔记笔记笔记笔记笔记笔记" 暗示了这是一份关于Java编程语言的学习笔记,可能包含了作者在阅读Java相关书籍时所做的重要记录和理解。笔记通常涵盖了语言的基础概念、核心特性、类与对象、内存管理...
涉及到了开发环境的搭建,包括FlexBuilder 3、MyEclipse、Tomcat、MySQL和BlazeDS的配置与使用,以及Hibernate的实体类、映射文件和配置文件的创建,为开发Flex与Java后端交互的应用程序提供了基础。
计算机网络设计第一章读书笔记------.pdf该文档详细且完整,值得借鉴下载使用,欢迎下载使用,有问题可以第一时间联系作者~
2020-4-6 java笔记 ---内部类 2020-4-6 java笔记 ---异常 2020-4-6 java笔记 --多线程 2020-4-8 java笔记 String类 2020-4-9 java 比较器 2020-4-10 java笔记 枚举类 2020-4-10 java 注解(Annotation) 2020-4-11 ...
从管理员、用户的功能要求出发,读书笔记共享平台系统中的功能模块主要是实现管理员;首页、个人中心、用户管理、笔记分享管理、个人笔记管理、管理员管理、交流互动、系统管理。用户:首页、个人中心、笔记分享管理...
云的学习笔记-云的学习笔记系统-云的学习笔记系统源码-云的学习笔记管理系统-云的学习笔记管理系统java代码-云的学习笔记系统设计与实现-基于ssm的云的学习笔记系统-基于Web的云的学习笔记系统设计与实现-云的学习...
Hibernate 通过实体类(Entity)、持久化属性(Fields)、映射文件(Hibernate Mapping File)等组件,建立了对象与数据库表之间的对应关系。开发者可以通过 Session 对象进行增删查改操作,Hibernate 会自动处理 ...
《java并发编程实战》读书笔记-第3章-对象的共享,脑图形式,使用xmind8制作 包括可见性、发布与逸出、线程封闭、不可变性、安全发布等内容