JPA的主要实现框架有哪些?代码举例讲解

JPA的主要实现框架有:

  1. Hibernate:功能最为强大和成熟的JPA实现框架。
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>5.4.17.Final</version>
</dependency>
  1. EclipseLink:Eclipse基金会出品的JPA实现框架。
<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>eclipselink</artifactId>
    <version>2.7.7</version>
</dependency>
  1. OpenJPA:Apache开源基金会出品的JPA实现框架。
<dependency>
    <groupId>org.apache.openjpa</groupId>
    <artifactId>openjpa</artifactId>
    <version>3.1.1</version>
</dependency>

上述JPA实现框架的配置和使用方式基本相同,示例代码:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence 
    http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd" 
    version="2.2">
    <persistence-unit name="example">
        <properties>
            <!-- 实现框架相关属性配置 -->
        </properties>
    </persistence-unit>
</persistence> 
EntityManagerFactory emf = Persistence.createEntityManagerFactory("example");  
EntityManager em = emf.createEntityManager();