本机持久性和休眠

时间:2012-12-31 11:33:24

标签: java hibernate jpa

如果我们可以使用javax.persistence完成本机java支持的ORM,那么使用hibernate等提供程序的优势是什么呢。*

本书java持久化与hibernate说,

Hibernate EntityManager is a wrapper around Hibernate Core that provides the
JPA programming interfaces, supports the JPA entity instance lifecycle, and allows
you to write queries with the standardized Java Persistence query language.

这里名称Hibernate EntityManager让我感到困惑。 EntityManager是否属于hibernate?

另外下面是如何持久化域对象throgh java persistence,

EntityManagerFactory emf =
Persistence.createEntityManagerFactory("helloworld");
// First unit of work
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();

以下是与

一起使用的persistence.xml文件
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="helloworld">
<properties>
<property name="hibernate.ejb.cfgfile"
value="/hibernate.cfg.xml"/>
</properties>
</persistence-unit>
</persistence>

所以这里再次持久单元声明了hibernate配置文件。那么对象是Persistence.createEntityManagerFactory(“helloworld”);实际上是在这里回归?

EntityManager em = emf.createEntityManager();

createEntityManager()方法的哪个对象成立?

1 个答案:

答案 0 :(得分:2)

JPA(Java Persistence Api)是一个规范(我认为JRE只有接口),可以由许多提供者实现。

因此,Hibernate(例如)使用hibernate核心提供了实现JPA规范的类。

使用JPA而不是Hibernate的原因是Hibernate是完全属性的;他们可以按照自己的意愿更改API,如果使用Hibernate API,则必须使用Hibernate作为ORM。 JPA是标准的,任何ORM提供者都可以选择满足JPA规范并通过它提供服务(例如EclipseLink)。

通过使用JPA,您可以轻松地切换您的ORM,以防您发现一些比您当前使用的更适合您(性能,错误修复等)。如果直接使用Hibernate,则无法轻松完成。