简体   繁体   English

没有外部持久性提供程序,我可以使用JPA吗?

[英]Can I use JPA without an external persistence provider?

I am trying to use JPA with Spring MVC, mySql. 我试图使用JPA与Spring MVC,mySql。 I understand from here What's the difference between JPA and Hibernate? 我从这里了解JPA和Hibernate有什么区别? that JPA may be used independently, but most web sources descibe it as only a specification, for which I need an implementation. JPA可以单独使用,但是大多数Web资源都将其描述为仅需要实现的规范。 So can I use JPA without persistence providers like EclipseLink, Hibernate etc and if so then How ? 那么我可以在没有EclipseLink,Hibernate等持久性提供程序的情况下使用JPA,如果是,那么如何?

Like you just said JPA is only a set of specifications. 就像您刚才所说的,JPA只是一组规范。 You cannot use the specifications, you can only use an implementation of those specifications and for this you need a persistance provide like Hibernate or EclipseLink... which implements JPA specifications. 您不能使用规范,只能使用那些规范的实现,为此,您需要持久性提供(如Hibernate或EclipseLink ...)来实现JPA规范。

You cannot use JPA without a provider. 如果没有提供程序,则无法使用JPA。 What you may do is to only use the standard features of JPA in your application to allow the persistence provider to be swapped easily (in theory). 您可能要做的是仅在应用程序中使用JPA的标准功能,以使持久性提供程序易于交换(理论上)。 In that case, only the JPA interfaces and annotations will be directly used by the client code. 在这种情况下,客户端代码将仅直接使用JPA接口和注释。

Another approach is to use the proprietary features of the persistence provider to extend JPA. 另一种方法是使用持久性提供程序的专有功能来扩展JPA。 For example, you may combine a proprietary annotation of the persistence provider with the standard JPA annotations to enable an optimization. 例如,您可以将持久性提供程序的专有注释与标准JPA注释相结合,以实现优化。 In that case, the client code depends directly on the persistence provider API. 在这种情况下,客户端代码直接依赖于持久性提供程序API。

In practice, it can be difficult to write JPA code that is 100% standard. 实际上,编写100%标准的JPA代码可能很困难。 I don't know for the other persistence providers, but Hibernate allows the use of some of its proprietary features from an EntityManager. 我不知道其他持久性提供程序,但是Hibernate允许使用EntityManager中的某些专有功能。

// Non-standard use of an HQL implicit select clause
Query query = em.createQuery("from Entity where ids in (:ids)");

// Non-standard use of a collection parameter in a native query
// Collection parameters are only standard for JPQL queries
Query query = em.createNativeQuery("select id from Table where id in (:ids)")
        .setParameter("ids", listOfIds);

And that's without taking into account how each persistence provider interact with the database. 而且没有考虑每个持久性提供程序如何与数据库交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM