JDO与JPA哪个更好?
今天偶然在网上看到了JDO,而我以前学的是JPA,那么两种有什么联系呢?或者说哪个更好?下面我先介绍两种的含义。
JDO的全文是:Java Data Object,就是对象持久化的新的规范。也是一个用于存取某种数据仓库中的对象的标准化API。JDO提供了透明的对象存储,因此对开发人员来说,存储数据对象完全不需要额外的代码(如JDBC API的使用)。这些繁琐的例行工作已经转移到JDO产品提供商身上,使开发人员解脱出来,从而集中时间和精力在业务逻辑上。另外,JDO很灵活,因为它可以在任何数据底层上运行。JDBC只是面向关系数据库(RDBMS)JDO更通用,提供到任何数据底层的存储功能,比如关系数据库、文件、XML以及对象数据库(ODBMS)等等,使得应用可移植性更强。、
JPA的全称是:Java Persistence API,其也是对象持久化的规范。JPA提供一种标准的OR映射解决方案,该解决方案完全集成到EJB3。0兼容的容器中。JPA的前辈将会继续稳定发展,但是业务应用程序中的 raw 使用将可能会减少。实现 JPA 兼容的实体管理器似乎很可能是此类技术的发展方向。
Java EE系列规范的较大问题与JPA没有任何关系。Java EE 系列规范的问题涉及到 Web和EJB容器之间的集成。Spring在此领域仍然具有主要竞争优势。JBoss的Seam项目尝试使用自定义的方法来解决这一问题。Caucho Resin应用服务器试图扩展容器边界并支持在Web容器中使用@EJB注释。我们希望Java EE 5.1将解决层集成的问题,为我们提供一个全面而标准的依赖性注入方法。
在不久的将来,Sun可能会将JPA作为一个单独的JSR对待,同时JPA还可能作为Java SE的一部分。不过这些都不太重要,重要的是,我们现在已经可以在脱离容器的情况下、在Java SE应用中使用JPA了。
JPA已经作为一项对象持久化的标准,不但可以获得Java EE应用服务器的支持,还可以直接在Java SE中使用。开发者将无需在现有多种ORM框架中艰难地选择,按照Sun的预想,现有ORM框架头顶的光环将渐渐暗淡,不再具有以往的吸引力。
两者都是一种规范,那么哪个会更强大呢?请看下表:
Feature
JDO
JPA
JDK Requirement
1.3+
1.5+
Usage
J2EE, J2SE
J2EE, J2SE
Persistence specification mechanism
XML, Annotations, API
XML, Annotations
Datastore supported
Any
RDBMS only
Restrictions on persisted classes
no-arg constructor (could be added by compiler/enhancer)
No final classes. No final methods. Non-private no-arg constructor. Identity Field. Version Field.
Ability to persist "transient" fields
Yes
No
Persist static/final fields
No
Not specified
Transactions
Pessimistic, Optimistic
Optimistic, some locking
Object Identity
datastore-identity, application-identity
application-identity
Object Identity generation
Sequence, Table, Identity, Auto, UUID String, UUID Hex
Sequence, Table, Identity, Auto
Change objects identity
Throw exception when not allowed
Undefined !!
Supported types
Java primitive types, wrappers of primitive types, java.lang.String, java.lang.Number, java.math.BigInteger, java.math.BigDecimal, java.util.Currency, java.util.Locale, java.util.Date, java.sql.Time, java.sql.Date, java.sql.Timestamp, java.io.Serializable, boolean[], byte[], char[], double[], float[], int[], long[], short[], java.lang.Object, interface, Boolean[], Byte[], Character[], Double[], Float[], Integer[], Long[], Short[], BigDecimal[], BigInteger[], String[],PersistenceCapable[], interface[], Object[], Enums, java.util.Collection, java.util.Set, java.util.List, java.util.Map,Collection/List/Map of simple types, Collection/List/Map of reference (interface/Object) types, Collection/List/Map of persistable types
Java primitive types, wrappers of the primitive types, java.lang.String, java.math.BigInteger, java.math.BigDecimal, java.util.Date, java.util.Calendar, java.sql.Date, java.sql.Time, java.sql.Timestamp, java.io.Serializable, byte[], Byte[], char[], Character[], Enums, java.util.Collection, java.util.Set, java.util.List, java.util.Map Collection/List/Map of persistable types
Embedded Fields
Embedded persistent objects, Embedded Collections, Embedded Maps
Embedded persistent objects
Access a non-detached field
Throw exception
Undefined !!
Inheritance
Each class has its own strategy
Root class defines the strategy
Operation cascade default
persist, (delete)
Operation Cascade configuration
delete
persist, delete, refresh
Query Language
JDOQL, SQL, others
JPQL, SQL
Query candidates
Candidate without subclasses, Candidate and its subclasses
Candidate and its subclasses
Query of candidate collection
yes
no
Query language case sensitivity
JDOQL lowercase/UPPERCASE
JPQL case-insensitive
Query language aliases
No, but has variables in JDOQL
Yes in JPQL
Query Criteria API
No, available as extension in QueryDSL
Yes
Object retrieval control
Lazy/Eager control, fetch groups
Lazy/Eager control
Bulk update/delete
JDOQL Bulk Delete
JPQL Bulk Delete, JPQL Bulk Update
RDBMS Schema Control
Tables, columns, PK columns, PK constraints, FK columns, FK constraints, index columns, index constraints, unique key columns, unique key constraints
Tables, columns, PK columns, FK columns, unique key columns
ORM Relationships
Full range of Collection, Map, List, Array, 1-1, 1-N, M-N using PC, Non-PC and interface objects
Basic 1-1, 1-N, M-N, Collection<NonPC>, Map<NonPC>
Default ORM column size
256
255
Default ORM identifiers (tables/columns)
No
Yes
Default ORM mappings
Yes, JDBC types defined for Java types
No
Caching interface
L2 Caching API
L2 Caching API
看完表,具体哪个更好,就要看个人需求了!