IntelliJ IDEA 2023.1 Help

Jakarta Persistence (JPA)

Jakarta Persistence (JPA), formerly known as Java Persistence API, is a Java specification for managing relational data in Java Enterprise applications.

Enable the Jakarta EE: Persistence ​(JPA)​ plugin

This functionality relies on the Jakarta EE: Persistence ​(JPA)​ plugin, which is bundled and enabled in IntelliJ IDEA by default. If the relevant features aren't available, make sure that you didn't disable the plugin.

  1. Press Ctrl+Alt+S to open the IDE settings and select Plugins.

  2. Open the Installed tab, find the Jakarta EE: Persistence ​(JPA)​ plugin, and select the checkbox next to the plugin name.

IntelliJ IDEA provides the following:

  • Coding assistance specific to JPA.

  • A dedicated facet for managing the JPA configuration persistence.xml and object-relational mapping orm.xml files.

  • The Persistence tool window for managing your JPA project items, creating configuration files and persistent classes, navigating to related source code in the editor, opening diagrams and consoles, and more.

  • Entity-relationship (ER) diagrams that you can access from the Persistence tool window.

  • An ability to generate managed entity classes and object-relational mappings for them by importing a database schema or an EJB deployment descriptor file ejb-jar.xml.

  • The JPA console for writing and running JPQL queries, and analyzing the query results.

Create a new Java Enterprise project with JPA

Since JPA is part of Jakarta EE (formerly known as Java EE), you can add support for it to any Java Enterprise application.

  1. Click New Project on the Welcome screen or select File | New | Project.

  2. From the Generators list, select Jakarta EE.

  3. Name the new project, select a build tool, a language you want to use, and select the Web application project template.

  4. From the JDK list, select the JDK that you want to use in your project.

    If the JDK is installed on your computer, but not defined in the IDE, select Add JDK and specify the path to the JDK home directory.

    If you don't have the necessary JDK on your computer, select Download JDK.

    Creating new project with JPA support
  5. On the next step of the wizard, select the Java Enterprise version to be supported.

  6. From the Dependencies list, select Persistence (JPA).

    If you are not going to implement all the interfaces of the JPA specification yourself, you also need to include a persistence framework. By default, IntelliJ IDEA provides support for the following persistence frameworks:

    • EclipseLink is the reference implementation. Select it if you are just trying things out.

    • Hibernate is the most popular implementation. For more information, see Hibernate.

    New Java Enterprise project with JPA and EclipseLink
  7. Click Create.

For more information on how to create a Java Enterprise project, refer to Tutorial: Your first Java EE application.

Enable JPA support for an existing project

  1. Open the build file in the editor (pom.xml or build.gradle depending on the build tool that you use in your project).

  2. Add the following dependency, but make sure to change the version according to your project's requirements:

    Jakarta EE
    <dependency> <groupId>jakarta.persistence</groupId> <artifactId>jakarta.persistence-api</artifactId> <version>3.1.0</version> </dependency>
    Java EE
    <dependency> <groupId>javax.persistence</groupId> <artifactId>javax.persistence-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency>
    Jakarta EE
    implementation 'jakarta.persistence:jakarta.persistence-api:3.1.0'
    Java EE
  3. Press Ctrl+Shift+O to import the changes.

For more information on how to work with build tools, see to Maven or Gradle.

Last modified: 19 December 2022