IntelliJ IDEA 2024.2 Help

Persistence tool window

If you have at least one module with the JPA facet in your project, you can use the Persistence tool window. Based on the dependencies of your project, it can show the following elements:

  • JPA: view and manage JPA items: configuration files, persistence units, managed entities (persistent classes and fields), Spring repositories, and JPA converters.

  • Spring Data MongoDB: view MongoDB @Document classes, generate new classes and fields from MongoDB documents.

  • Liquibase: view Liquibase changelogs, create new ones, and run Liquibase update.

The Persistence tool window

Open Entity in Persistence view

  1. In the gutter, click an entity icon The Entity icon. If you want to navigate to a particular field, click the corresponding icon next to it, such as The attribute icon or The reference icon.

  2. Select Select in Persistence View.

Select in Persistence View

Alternatively, place the caret at your entity source code and click in the Persistence tool window.

Navigate to entity definitions

To navigate from an entity in the Persistence tool window to the corresponding source code definition in the editor, do one of the following:

  • Double-click a field.

  • Select a field or class and press F4.

  • Right-click a field or class and select Jump to Source.

View entity relationship diagrams (ERD)

  • Right-click a managed entity and select Entity Relationship Diagram.

Associate persistence units with data sources

  1. Right-click a module, persistence unit, or entity, and select Assign Data Sources.

  2. Select necessary data sources for relevant persistence units and click OK.

With this association, IntelliJ IDEA will validate the source code that references database tables against the corresponding data sources. Also, the JPA console will use proper database connection settings, and you won't need to specify them manually.

Associate persistence units with naming strategies

  1. Right-click a module, persistence unit, or entity, and select Assign Naming Strategies.

  2. Select necessary naming strategies for relevant persistence units and click OK.

Run JPQL queries

  • Right-click a persistence unit or entity and select Console or click The Console button in the toolbar Ctrl+Shift+F10.

This will open the JPA console where you can write and execute JPQL queries.

Generate entities from database

With IntelliJ IDEA, you can create JPA entity classes and attributes based on an existing database schema. This functionality relies on the Reverse Engineering plugin, which is bundled in IntelliJ IDEA Ultimate.

Generate JPA entities from database

  1. If a database connection is not established, create one.

  2. In the Persistence tool window, expand the JPA node, right-click an element, and select New | JPA Entities from DB.

    Alternatively, right-click a database connection in the Database tool window, and select Create JPA Entities from DB.

  3. Select a database connection, tables, and attributes to be mapped. For more information, refer to Entities from DB Wizard.

While your IDE is open, the database can be modified by other clients. To get the latest data from the database, you can click in either the Entities from DB window or in the Database tool window.

Generate attributes from database

You can also generate attributes for an existing class based on a database table.

  1. If a database connection is not established, create one.

  2. In the Persistence tool window, expand the JPA node, right-click an entity and select New | JPA Entities from DB.

    Alternatively, right-click a database connection in the Database tool window, and select Create JPA Attributes from DB.

  3. Select a database connection, a table or a view, and columns to be mapped. The attributes migration flow is identical to what is described in the Entities from DB wizard section.

    Entity attributes from DB dialog

Generate @Document classes from MongoDB

With IntelliJ IDEA, you can create @Document classes and attributes based on an existing collections in MongoDB. This functionality relies on the Reverse Engineering plugin, which is bundled in IntelliJ IDEA Ultimate.

Generate Document classes from MongoDB

  1. If a MongoDB connection is not established, create one.

  2. In the Persistence tool window, expand the Spring Data MongoDB node, right-click an element, and select New | MongoDB Documents from DB.

    MongoDB Documents from DB

    Alternatively, right-click a MongoDB connection in the Database tool window, and select Create MongoDB Documents from DB.

  3. Select a database connection, tables, and attributes to be mapped. For more information, refer to Entities from DB Wizard.

    MongoDB Documents from DB

While your IDE is open, the database can be modified by other clients. To get the latest data from the database, you can click in either the Entities from DB window or in the Database tool window.

Generate attributes from MongoDB

You can also generate attributes for an existing class based on a MongoDB table.

  1. If a database connection is not established, create one.

  2. In the Persistence tool window, expand the Spring Data MongoDB node, right-click an entity and select New | MongoDB Fields from DB.

    MongoDB Documents from DB

    Alternatively, right-click a MongoDB connection in the Database tool window, and select Create MongoDB Fields from DB.

  3. Select a database connection, a table or a view, and columns to be mapped. The attributes migration flow is identical to what is described in the Entities from DB wizard section.

    MongoDB Documents from DB
Last modified: 11 October 2024