JPA Cannot resolve column/IntelliJ

2019-03-14 19:50发布

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,

4条回答
爷的心禁止访问
2楼-- · 2019-03-14 20:34

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.

查看更多
趁早两清
3楼-- · 2019-03-14 20:44

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.

查看更多
唯我独甜
4楼-- · 2019-03-14 20:44

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.

查看更多
叼着烟拽天下
5楼-- · 2019-03-14 20:47

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

查看更多
登录 后发表回答