IntelliJ IDEA 2016.3 Help

Enabling JPA Support

This feature is supported in Ultimate edition only.

Overview

To be able to use JPA support, you should:

  • Make sure that the Java EE: EJB, JPA, Servlets plugin is enabled. (This plugin is bundled with the IDE and enabled by default.)
  • Enable JPA support at a module level. You can do that when creating a new project or module. You can also enable JPA support for an existing module. In all such cases, IntelliJ IDEA will (some of the following will be offered as options):
    • Create a JPA configuration file persistence.xml.
    • Download the library files that implement the framework and add them to the dependencies of the corresponding module.
    • Generate entity classes and object/relational mappings for your database tables (if you have an appropriate data source available).
    • Create a JPA facet. You'll be able to use that facet for specifying the default configuration and object-relational mapping files, and JPA implementation provider.
    • Make the Persistence tool window available.

Making sure that the Java EE: EJB, JPA, Servlets plugin is enabled

  1. Open the Settings / Preferences dialog (e.g. Ctrl+Alt+S).
  2. In the left-hand part of the dialog, select Plugins.
  3. In the right-hand part of the dialog, on the Plugins page, type jpa in the search box. As a result, only the plugins whose names and descriptions contain jpa are shown.
  4. If the check box to the right of Java EE: EJB, JPA, Servlets is not selected, select it.
  5. Click OK.
  6. If suggested, restart IntelliJ IDEA.

Enabling JPA support when creating a project or module

  1. Do one of the following:
    • If you are going to create a new project: click Create New Project on the Welcome screen or select File | New | Project.

      As a result, the New Project wizard opens.

    • If you are going to add a module to an existing project: open the project you want to add a module to, and select File | New | Module.

      As a result, the New Module wizard opens.

  2. On the first page of the wizard, in the left-hand pane, select Java. In the right-hand part of the page, specify the JDK that you are going to use.
  3. Under Additional Libraries and Frameworks, select the JavaEE Persistence check box.
  4. Select the version of persistence.xml to be created. (persistence.xml is a JPA configuration file.)

    If necessary, select the Java persistence provider, i.e. the JPA implementation provider. (This setting, initially, will only affect the <provider> element in persistence.xml.)

    If there is a database defined in IntelliJ IDEA as a data source, you can select to import the database schema. This will result in creating the necessary JPA entity classes and object/relational mappings for them.

  5. Select the required library option and, if necessary, specify the associated settings. You can choose to:
    • Download JPA implementation files and arrange those files in a library.

      To do that, under Libraries, select Download. Optionally, click Configure to edit the library settings. (The Downloading Options dialog will open.)

    • Use a library that is already defined IntelliJ IDEA.

      To do that, click Use library and select the required library from the list. Optionally, click Configure to edit the library settings. (The Edit Library dialog will open.)

    • Create a new library using the appropriate JAR files available on your computer.

      To do that, click Use library and then click Create. Select the required JAR files in the dialog that opens. (For multiple selection, keep the Ctrl key pressed.) Optionally, click Configure to edit the library settings. (The Create Library dialog will open.)

    • Postpone setting up the library until a later time. In this case, select Set up library later.

    Click Next.

  6. Specify the name and location settings. For more information, see Project Name and Location or Module Name and Location.

    Click Finish.

If you selected to import a database schema, the Import Database Schema dialog opens, and you can specify the entity classes to be generated and associated settings.

Enabling JPA support for an existing module

  1. In the Project tool window, right-click the necessary module and select Add Framework Support.
  2. In the left-hand pane of the Add Frameworks Support dialog that opens, select the JavaEE Persistence check box.
  3. Select the version of persistence.xml to be created. (persistence.xml is a JPA configuration file.)

    If necessary, select the Java persistence provider, i.e. the JPA implementation provider. (This setting, initially, will only affect the <provider> element in persistence.xml.)

    If there is a database defined in IntelliJ IDEA as a data source, you can select to import the database schema. This will result in creating the necessary JPA entity classes and object/relational mappings for them.

  4. Select the required library option and, if necessary, specify the associated settings. You can choose to:
    • Download JPA implementation files and arrange those files in a library.

      To do that, under Libraries, select Download. Optionally, click Configure to edit the library settings. (The Downloading Options dialog will open.)

    • Use a library that is already defined IntelliJ IDEA.

      To do that, click Use library and select the required library from the list. Optionally, click Configure to edit the library settings. (The Edit Library dialog will open.)

    • Create a new library using the appropriate JAR files available on your computer.

      To do that, click Use library and then click Create. Select the required JAR files in the dialog that opens. (For multiple selection, keep the Ctrl key pressed.) Optionally, click Configure to edit the library settings. (The Create Library dialog will open.)

    • Postpone setting up the library until a later time. In this case, select Set up library later.
  5. Click OK.

If you selected to import a database schema, the Import Database Schema dialog opens, and you can specify the entity classes to be generated and associated settings.

See Also

Last modified: 7 December 2016