69

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,

1
  • 1
    All these answers are so complicating life. Today the most obvious solution is right-click the column or table, select the data source and it's fixed * eye roll emoticon *. Jun 6, 2021 at 14:04

10 Answers 10

127

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 none.)

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

8
  • 6
    Correct answer, because it explains the behavior and offers a solution, while the other just tells you to ignore it.
    – sschrass
    Sep 11, 2015 at 8:55
  • 12
    @panos This only fixes @Table errors, but my columns still show as not able to resolve. Any idea how to fix that?
    – Don Rhummy
    May 24, 2016 at 14:08
  • Something else you can check, is that the name of the generated/coded entity class, has the same name than the table in database...if it does not, use name propety of Entity annotation to to match the table. For example: @Entity(name="yourTableName")
    – demian
    Oct 19, 2016 at 2:21
  • 5
    I have done that and only Entity name is identifiable, but, the Column name is still unexpected, how to get rid of this? May 13, 2018 at 13:32
  • 1
    When it comes to an IntelliJ issue, the answers will mostly "Invalidate cache and Restart". :P This is the first answer I've ever seen with with a proper fix. Jan 30, 2020 at 5:25
44

For those who just want to disable this check:

go to IntelliJ IDEA -> Preferences -> Search "Unresolved database references in annotations" and uncheck it.

IDE Inspection Settings (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.

2
  • 1
    thanks. this check is so annoying when using several datasources
    – Appyx
    Jan 8, 2020 at 9:27
  • Thank you for this. Please send a Karen to go complain to the Project Manager at JetBrains who thought this feature should be enabled by default. Jan 29, 2021 at 11:33
36

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.

2
  • The righthand column under Assign Data Sources is freakin hard to see! Didn't even notice it till now! Thank you!
    – Alkanshel
    Jan 4, 2018 at 22:17
  • It is Steven's answer that makes sense, not assuming things when it is related to configuration. It must be mentioned. Sep 28, 2018 at 8:31
5

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.

1
  • Also did the trick for me. But there must be some kind of misconfiguration between the naming strategy and I don't know what, because it supposed to work without specifying that.
    – Mike
    Jun 4, 2020 at 13:52
2

I had the same error even though I had specified the data source in every possible place.

The problem was that I had changed the database schema and the Database tab in Intellij was still containing the old schema, so I just had to refresh the Database tab.

You can resolve it by going to View -> Tool Windows -> Database and then just click the refresh button or F5.

1

Actually this is not any error that prevents your code to compile. Probably your spell-checker is on and which gives you the spelling mistake. If you can compile your code then you may ignore these type of scenario.

Look you have not getting any error for other text like @Column, @GeneratedValue etc. That means jars using these kind of stuff are in your build path. So I think you can ignore these type of error.

1
  • 3
    Thanks for the answer. But why is @Column(name = myConstant) acceptable? myConstant is something like 'static final String myConstant = "LEC_ID") Aren't hardcoded Strings and constants the same in Java?
    – pichlbaer
    Mar 19, 2015 at 21:57
1

maybe this solution also help someone...

  1. Open Database dialog window from the right side of Intellij
  2. Go to DB Data Source Properties (find it in top menu)
  3. Go to Schemas
  4. Uncheck "Default database"
  5. Check your specific DB and inside also check Default schema(public)

Good luck!

0

If you are using the JPA, it is also important to set up the SQL Dialect as HSQLDB instead of MySQL. This is often confusing because it feels like MySQL is the correct dialect, but in fact what you are dealing with hibernate is HSQL.

If you have just upgraded to IntelliJ 2017.13 or other derivatives of this version (e.g., Webstorm), or you've just re-imported your project, it might mess up the language injection and need to manually correct this.

This can be accomplished via the Project

Settings -> Languages and Frameworks

part of the IDE.

1
  • This is incorrect, HSQLDB is something called HyperSQL and is unrelated to hibernate. But it does seem like there is some configuration issue after upgrading to IDEA 2017, I was getting "cannot resolve column" after importing my config to it.
    – Alkanshel
    Jan 4, 2018 at 22:02
0

If there is no real mismatch and this warning happens just because of IDE, you can easily link your project with the data source that you connect. Intellij already suggests the way to do it.

0

I'm using Intellij and connecting DB by JPA. I've met problem just like you and this is my solution.

  1. You need to connect your database following these steps:
  • View -> Tool Windows -> Database -> then add database you are using to Intellj
  1. After connect your database, you need to assign data source to Intellj by following these
  • View -> Tool Windows -> Persistence -> then add data from database you just added from 1 step OK. That's all! P/s: make sure your code work well!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.