I am trying to use JPA with Spring MVC, mySql. I understand from here What's the difference between JPA and Hibernate? that JPA may be used independently, but most web sources descibe it as only a specification, for which I need an implementation. So can I use JPA without persistence providers like EclipseLink, Hibernate etc and if so then How ?
2 Answers
Like you just said JPA is only a set of specifications. 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.

- 361
- 1
- 7
You cannot use JPA without a provider. 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). In that case, only the JPA interfaces and annotations will be directly used by the client code.
Another approach is to use the proprietary features of the persistence provider to extend JPA. For example, you may combine a proprietary annotation of the persistence provider with the standard JPA annotations to enable an optimization. In that case, the client code depends directly on the persistence provider API.
In practice, it can be difficult to write JPA code that is 100% standard. I don't know for the other persistence providers, but Hibernate allows the use of some of its proprietary features from an 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.

- 846
- 6
- 8