IntelliJ IDEA 2021.2 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.

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. From the main menu, select File | New | Project.

  2. In the New Project dialog, select Java Enterprise.

    New Java Enterprise project wizard

    Select the necessary settings and click Next to continue.

  3. In the Dependencies list, select the Persistence (JPA) specification.

    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

    Click Next.

  4. Enter a name for your project and click Finish.

IntelliJ IDEA creates the default project structure with the JPA facet and all the necessary libraries as external dependencies, such as javax.persistence for the JPA specification and org.eclipse.persistence for the EclipseLink framework. If you specified an application server, IntelliJ IDEA will also create a run configuration to start the server, build and deploy the artifact.

Enable JPA support for an existing Java Enterprise project

If you already have a Java Enterprise application, you can add JPA as a facet.

  1. In the Project tool window, right-click the necessary module and select Add Framework Support.

  2. Select JavaEE Persistence and choose whether you want to use an existing library, create a new one, or configure the library later.

    You can also select the version of the JPA configuration file persistence.xml, select the default persistence provider, and choose whether you want to import the database schema from a defined data source.

  3. Click OK to apply the changes to your project.

Alternatively, you can open the Project Structure dialog Ctrl+Alt+Shift+S, then open Modules, select the necessary module, click the Add button, and then find and add JPA. You will see the new JPA facet under Facets.

Last modified: 15 June 2021