How to enable JPA support in IntelliJ IDEA

JPA support is enabled by default but might not be configured correctly. This leads to red lines in code sections that use JPA, namely the database entities, and IDEA complaining about these errors on every commit although there is no problem. To enable JPA support with Spring, you can do the following:

  1. Add the development database to the project:

    1. Select menu:View[Tool Windows > Database] to open the Database View.

    2. Using the btn:[+] button, select menu:Data Source[PostgreSQL].

    3. Fill in the settings to connect to your local (Docker-based) PostgreSQL database. Most of the defaults should be correct. (If in doubt, "postgres" is most likely the right value.) Install the PostgreSQL driver if you have not done so already. Test the connection using the "Test Connection" button. (Remember that the Docker container needs to be running.) When a connection can be established, save the settings with btn:[OK].

  2. Connect the data source to the Persistence manager:

    1. Select menu:View[Tool Windows > Persistence] to open the Persistence View. You should see one entry for the "main" module of the project.

    2. Expand the entries. You should see the "entityManagerFactory" component and a list of all JPA entities defined in the project.

    3. Right-click on "entityManagerFactory" and select "Assign Data Sources…" from the menu.

    4. In the window, click into the empty field in the "Data Source" column. Select the entry of the previously defined data source (labeled "postgres@localhost" by default). Click btn:[OK].

    5. Select and entity and press kbd:[F4] (Go to Source). Verify that the errors on the @Column annotations are gone. If you still see errors, try starting the API (bootRun task in Gradle) to update your database to the latest schema.