JPA Cannot resolve column/IntelliJ

I'm trying to map some Java classes using the Hibernate JPA implementation. My problem is that I can't use hardcoded Strings als column namens. You can see the error message I get in the picture below.

enter image description here

I'm using OpenJPA as my Default JPA Provider and have already tried to change it.

IntelliJ Version 14.0.3

regards,


Solution 1:

You have to associate your data source (create a data source in database view first - aka your real JDBC database connection) with a persistence unit or session factory (hibernate.cfg.xml).

Follow below steps and the warnings will disappear:

  1. Open the Persistence tool window (View | Tool Windows | Persistence).
  2. Right-click the necessary module, persistence unit or session factory, and select Assign Data Sources in the context menu.
  3. In the Assign Data Sources dialog that opens, click the Data Source field and select the necessary data source. (To remove the association with the data source, select .)

See here: Associating data sources with session factories and persistence units

Solution 2:

For those who just want to disable this check, go to Intellij IDEA -> Preferences -> Search "Unresolved database references in annotations" and uncheck it. enter image description here (macOS Mojave and Intellij Ultimate 2019.3)
This will disable the inspection and remove all the "Cannot resolve column…" errors on the @Column annotations. Intellij will stop checking if the String column names exist in the database tables. Do it at your own risk.

Solution 3:

I found Panos' answer useful, but I didn't see anybody mention adding the database as a data source. Perhaps that's routine enough to be assumed, but if not, here's what I had to do:

Select View/Tool Windows/Database

The Database window usually appears on the top right.

In the Database window, click the green + sign and select Data Source/MySQL (or whatever flavor of Data Source you're using).

The Data Sources and Drivers window pops up.

If your database isn't listed there, use the empty form to add the following:

  • Host: usually localhost, but if your test database is living on another machine, put that address in.
  • Username: for your database use.

  • Password: for your database user's password.

IDEA might need some fiddling to find the JDBC driver. I was told that in theory it should have found it in the maven build process for the project I was working on, but it did not. I reopened View/Tool Windows/Database and looked at my MySQL entry. At the bottom it had an error message next to Driver:, and also a MySQL link. I clicked the MySQL link and IDEA brought up a popup to fetch Connector/J.

Despite the fact that the checkboxes for Auto commit and Auto sync defaulted to checked and I left them that way, IDEA seemed to need a nudge. Or perhaps it was just taking a while and I was impatient.

In any event, first I double-clicked on the line for my database in Database. That seemed to do it, but I didn't realize I needed Persistence yet, and while sorting that out, at a coworker's suggestion, I also clicked the Synchronize button (two arrows in a circle) on Database.

You can also right-click on your database in Database and select Synchronize.

It may take a few seconds, but you should see IDEA filling in the database schema under the entry in Database.

Finally I found Panos's answer and fixed Persistence.

Select View/Tool Windows/Persistence

The Persistence window usually appears on the top left.

In the Persistence window, right-click on your project and select Assign Data Sources.

IDEA pops up a dialog with two columns, your project in the left column and in my case an empty cell in the right column. Click on the empty cell and IDEA should give you a dropdown that allows you to select the database you just added.

Again, it may take a few seconds for IDEA to finish analyzing the data source and redo all the inspections.

Solution 4:

Just for anyone else whom this didn't solve and comes across via google (like myself) .. setting the table name via the @Table Annotation fixed it for me.