`
darrenzhu
  • 浏览: 797637 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

EJB3.0,JPA,Hibernate之间的关系

阅读更多
1.EJB3.0和JAP之间的关系
EJB3.0是一份规范,该规范由不同的部分组成:
第一部分为session bean和message-driven bean定义了新的编程模型,以及部署规则等等;
第二部分专门定义了持久化相关的规范:实体,对象/关系映射元数据,持久化管理接口和查询语言。第二部分就是我们所说的JPA(Java Persistence API),之所以取名叫JPA,很有可能是因为持久化的接口位于javax.persistence.
所以,JPA是EJB的一部分,是EJB专门为持久化定义的规范。

2.Hibernate和EJB3.0之间的关系
首先你必须要了解的是,一个规范和一个产品是没有太多可比较性的,EJB3.0是java服务器端组件模型的一份规范,而hibernate是一个具体的产品,所以准确的提问应该是:hibernate实现了EJB3.0的规范吗?

正如EJB3.0的规范划分成了不同的部分一样,EJB的实现者也有区分,有些产品完全实现了EJB3.0的规范,而有些产品只是实现了EJB3.0的一部分,比如仅实现了Java Persistence部分。
Hibernate就是这样的产品,它实现了Java Persistence那部分规范,不仅如此,而且它还提供了一些Java Persistence规范里面没有的一些功能。所以也可以说,JPA规范所对应的功能是hibernate的子集。

3.如何判断你使用的功能是JPA描述的还是hibernate自己特有的呢?
一个简单的方式就是检查你引入的包,如果你只使用了javax.persistence.*,那么你使用的功能是通用的JPA提供的,如果你的代码里还引入了org.hibernate.*,那么你就使用了hibernate专有的功能。

4.实现JPA的不仅仅只有Hibernate EntityManager,还有TopLink,OpenJPA.

5.JPA已经作为一项对象持久化的标准,不但可以获得Java EE应用服务器的支持,还可以直接在Java SE中使用。开发者将无需在现有多种ORM框架中艰难地选择,按照Sun的预想,现有ORM框架头顶的光环将渐渐暗淡,不再具有以往的吸引力。









英文内容摘取自《Hibernate in action》(the second edition) 第1.4.4小节
what exactly is the relationship
between Hibernate and EJB3, and what is Java Persistence?


Understanding the standards
First, it’s difficult (if not impossible) to compare a specification and a product. The
questions that should be asked are, “Does Hibernate implement the EJB 3.0 specification,
and what is the impact on my project? Do I have to use one or the other?”
The new EJB 3.0 specification comes in several parts: The first part defines
the new EJB programming model for session beans and message-driven beans,
the deployment rules, and so on. The second part of the specification deals with
persistence exclusively: entities, object/relational mapping metadata, persistence manager interfaces, and the query language. This second part is called Java Persistence
API (JPA), probably because its interfaces are in the package
javax.persistence. We’ll use this acronym throughout the book.
This separation also exists in EJB 3.0 products; some implement a full EJB 3.0
container that supports all parts of the specification, and other products may
implement only the Java Persistence part. Two important principles were
designed into the new standard:
■ JPA engines should be pluggable, which means you should be able to take
out one product and replace it with another if you aren’t satisfied—even if
you want to stay with the same EJB 3.0 container or Java EE 5.0 application
server.
■ JPA engines should be able to run outside of an EJB 3.0 (or any other) runtime
environment, without a container in plain standard Java.
The consequences of this design are that there are more options for developers
and architects, which drives competition and therefore improves overall quality of
products. Of course, actual products also offer features that go beyond the specification
as vendor-specific extensions (such as for performance tuning, or because
the vendor has a focus on a particular vertical problem space).
Hibernate implements Java Persistence, and because a JPA engine must be
pluggable, new and interesting combinations of software are possible. You can
select from various Hibernate software modules and combine them depending on
your project’s technical and business requirements.

Hibernate Core
The Hibernate Core is also known as Hibernate 3.2.x, or Hibernate. It’s the base
service for persistence, with its native API and its mapping metadata stored in XML
files. It has a query language called HQL (almost the same as SQL), as well as programmatic
query interfaces for Criteria and Example queries. There are hundreds
of options and features available for everything, as Hibernate Core is really
the foundation and the platform all other modules are built on.
You can use Hibernate Core on its own, independent from any framework or
any particular runtime environment with all JDKs. It works in every Java EE/J2EE
application server, in Swing applications, in a simple servlet container, and so on.
As long as you can configure a data source for Hibernate, it works. Your application
code (in your persistence layer) will use Hibernate APIs and queries, and
your mapping metadata is written in native Hibernate XML files.
Native Hibernate APIs, queries, and XML mapping files are the primary focus
of this book, and they’re explained first in all code examples. The reason for that
is that Hibernate functionality is a superset of all other available options


Hibernate Annotations
A new way to define application metadata became available with JDK 5.0: type-safe
annotations embedded directly in the Java source code. Many Hibernate users are
already familiar with this concept, as the XDoclet software supports Javadoc metadata
attributes and a preprocessor at compile time (which, for Hibernate, generates
XML mapping files).
With the Hibernate Annotations package on top of Hibernate Core, you can now
use type-safe JDK 5.0 metadata as a replacement or in addition to native Hibernate
XML mapping files. You’ll find the syntax and semantics of the mapping annotations
familiar once you’ve seen them side-by-side with Hibernate XML mapping
files. However, the basic annotations aren’t proprietary.
The JPA specification defines object/relational mapping metadata syntax and
semantics, with the primary mechanism being JDK 5.0 annotations. (Yes, JDK 5.0
is required for Java EE 5.0 and EJB 3.0.) Naturally, the Hibernate Annotations are
a set of basic annotations that implement the JPA standard, and they’re also a set
of extension annotations you need for more advanced and exotic Hibernate
mappings and tuning.
You can use Hibernate Core and Hibernate Annotations to reduce your lines
of code for mapping metadata, compared to the native XML files, and you may
like the better refactoring capabilities of annotations. You can use only JPA annotations,
or you can add a Hibernate extension annotation if complete portability
isn’t your primary concern. (In practice, you should embrace the product you’ve
chosen instead of denying its existence at all times.)
We’ll discuss the impact of annotations on your development process, and how
to use them in mappings, throughout this book, along with native Hibernate XML
mapping examples.


Hibernate EntityManager
The JPA specification also defines programming interfaces, lifecycle rules for persistent
objects, and query features. The Hibernate implementation for this part of
JPA is available as Hibernate EntityManager, another optional module you can
stack on top of Hibernate Core. You can fall back when a plain Hibernate
interface, or even a JDBC Connection is needed. Hibernate’s native features are a
superset of the JPA persistence features in every respect. (The simple fact is that

Hibernate EntityManager is a small wrapper around Hibernate Core that provides
JPA compatibility.)
Working with standardized interfaces and using a standardized query language
has the benefit that you can execute your JPA-compatible persistence layer with
any EJB 3.0 compliant application server. Or, you can use JPA outside of any particular
standardized runtime environment in plain Java (which really means everywhere
Hibernate Core can be used).
Hibernate Annotations should be considered in combination with Hibernate
EntityManager. It’s unusual that you’d write your application code against JPA
interfaces and with JPA queries, and not create most of your mappings with JPA
annotations.


Java EE 5.0 application servers
We don’t cover all of EJB 3.0 in this book; our focus is naturally on persistence,
and therefore on the JPA part of the specification. (We will, of course, show you
many techniques with managed EJB components when we talk about application
architecture and design.)
Hibernate is also part of the JBoss Application Server (JBoss AS), an implementation
of J2EE 1.4 and (soon) Java EE 5.0. A combination of Hibernate Core, Hibernate
Annotations, and Hibernate EntityManager forms the persistence engine of
this application server. Hence, everything you can use stand-alone, you can also
use inside the application server with all the EJB 3.0 benefits, such as session
beans, message-driven beans, and other Java EE services.
To complete the picture, you also have to understand that Java EE 5.0 application
servers are no longer the monolithic beasts of the J2EE 1.4 era. In fact, the
JBoss EJB 3.0 container also comes in an embeddable version, which runs inside
other application servers, and even in Tomcat, or in a unit test, or a Swing application.
In the next chapter, you’ll prepare a project that utilizes EJB 3.0 components,
and you’ll install the JBoss server for easy integration testing.
As you can see, native Hibernate features implement significant parts of the
specification or are natural vendor extensions, offering additional functionality if
required.
Here is a simple trick to see immediately what code you’re looking at, whether
JPA or native Hibernate. If only the javax.persistence.* import is visible, you’re
working inside the specification; if you also import org.hibernate.*, you’re
using native Hibernate functionality. We’ll later show you a few more tricks that
will help you cleanly separate portable from vendor-specific code.
分享到:
评论

相关推荐

    《EJB 3.0入门经典》 源码

    2. **简化持久化**:引入了JPA(Java Persistence API),使对象/关系映射变得更加简单,开发者可以使用ORM框架如Hibernate,通过注解定义实体类与数据库表的映射。 3. **无容器依赖的POJOs**:EJB 3.0的实体Bean...

    EJB学习大全(EJB3.0实例教程 JPA教程 实战EJB)

    5. **持久化框架(Persistence Framework)**:EJB3.0引入了JPA(Java Persistence API),提供了一种标准的方式来管理和操作数据,与ORM工具如Hibernate集成。 **JPA教程**: JPA是Java平台上的持久化规范,它允许...

    ejb3.0 jpa

    JPA由EJB 3.0软件专家组开发,作为JSR-220实现的一部分。但它不囿于EJB 3.0,你可以在Web应用、甚至桌面应用中使用。JPA的宗旨是为POJO提供持久化标准规范,由此可见,经过这几年的实践探索,能够脱离容器独立运 行...

    ejb3.0规范PDF

    综上所述,ejb3.0规范PDF包含了大量的信息,从配置简化到实体Bean的JPA集成,再到会话Bean、消息驱动Bean的使用,以及依赖注入、接口驱动编程、事务管理和安全性等多个方面,为Java EE开发者提供了全面的指导。...

    EJB3.0实例教程(源代码).rar )

    EJB 3.0引入了Java Persistence API(JPA),使得实体bean的管理更加简单,可以与ORM(对象关系映射)框架如Hibernate集成。 **3. EJB 3.0新特性** - **注解驱动**:例如,`@Entity`用于标记实体bean,`@EJB`用于...

    EJB3.0 ——黎活明

    黎活明老师的这本书《EJB3.0》是国内首部全面解析EJB 3.0技术的专业著作,旨在帮助读者深入理解这一核心技术。 ### 1. EJB 3.0的核心概念 - **实体Bean(Entity Beans)**:EJB 3.0中的实体Bean不再需要编写复杂的...

    演示EJB3.0 + JPA + MySQL5.0 + C3P0连接池技术实战编程(Top-Down的XP开发方式)

    如果运行一切正常,那么你会看到使用EJB 3.0组件与JPA技术层技术完成的Hello world演示应用。 注意:配置JBoss服务器和调试的动作参见readme.txt文档,有详细说明怎样匹配连接池,以及可能遇到的问题及解决办法。该...

    EJB 3.0 持久性 API

    在EJB 3.0版本中,持久性API的引入极大地简化了对象关系映射(ORM),使得开发者可以更加专注于业务逻辑,而无需过多地关注数据库交互的细节。以下是关于EJB 3.0持久性API的详细知识点: 1. **注解驱动的持久化**:...

    ejb3.0+Weblogic+Jboss安装配置及入门例子

    2. 持久化:通过`@Entity`注解,EJB3.0提供了更直观的持久化模型,支持JPA(Java Persistence API),并与Hibernate等ORM框架集成。 3. 自动事务管理:EJB容器自动处理事务,开发者无需关心底层的事务控制。 4. 无...

    《EJB3.0入门经典教程精简版》源码

    4. **持久化框架(Persistence Framework)**:EJB3.0集成了JPA(Java Persistence API),使得开发者可以使用ORM(Object-Relational Mapping)工具,如Hibernate,进行数据持久化。`@PersistenceContext`用于注入...

    精通EJB 3.0

    3. 自动持久化:通过@Entity注解,EJB 3.0引入了Java Persistence API(JPA),使得对象关系映射(ORM)变得更加简单。开发者可以通过注解来定义实体类和数据库表之间的映射,无需编写繁琐的Hibernate XML配置。 4....

    EJB3.0培训教材

    4. **持久化支持**:EJB3.0集成JPA(Java Persistence API),提供了对象关系映射(ORM)功能,简化了数据库访问。 【EJB3.0与Spring的对比】 虽然Spring和Hibernate等框架提供了与EJB类似的服务,例如事务管理和...

    EJB3.0与EJB2.0的区别

    本文将详细探讨EJB3.0与EJB2.0之间的主要差异。 首先,EJB3.0对编程模型进行了简化。在EJB2.0中,每个EJB需要定义两个接口和一个Bean实现类,这在项目规模较大时会增加代码的复杂性。EJB3.0通过引入annotations(元...

    ejb3.0实例教程 好的没话说 超级清新 不下后悔 ejb3.0教程

    此外,支持JPA(Java Persistence API),可以使用ORM(Object-Relational Mapping)工具如Hibernate或EclipseLink进行数据库操作。 2. **会话Bean(Session Beans)**:会话Bean在EJB 3.0中也变得更加简单,可以...

    达内EJB3.0精典

    5. **依赖注入**:EJB3.0支持JSR 330标准的依赖注入(Dependency Injection),如`@Inject`和`@Named`,使得组件之间可以通过容器进行依赖关系的管理,降低了代码的耦合度。 6. **查询语言**:EJB3.0引入了JPQL...

    EJB3.0实例教程源码

    本实例教程将专注于EJB 3.0中的多对多映射,这是关系数据库与对象模型之间关系映射的一种常见模式。 多对多映射在现实世界中很常见,例如在学生和老师的关系中,一个学生可以有多个老师,同时一个老师也可以教多个...

    EJB3.0 的两本书

    - EJB 3.0集成了JPA,使得开发者可以方便地进行对象持久化,同时支持多种持久化供应商,如Hibernate和OpenJPA。 6. **查询语言** - EJB 3.0引入了JPQL(Java Persistence Query Language),类似于SQL,但面向...

    EJB3.0 实例编程

    EJB3.0与JSF、Spring、Hibernate等其他Java框架很好地集成,使得开发者可以根据项目需求灵活选择技术和架构。 通过深入学习EJB3.0实例编程,开发者可以掌握如何有效地利用这些特性构建企业级应用,提升开发效率,...

Global site tag (gtag.js) - Google Analytics