Java JPA 无法解析列/IntelliJ
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29155350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA Cannot resolve column/IntelliJ
提问by pichlbaer
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.
我正在尝试使用 Hibernate JPA 实现映射一些 Java 类。我的问题是我不能使用硬编码的字符串 als 列名称。您可以在下图中看到我收到的错误消息。
I'm using OpenJPA as my Default JPA Provider and have already tried to change it.
我使用 OpenJPA 作为我的默认 JPA 提供程序并且已经尝试更改它。
IntelliJ Version 14.0.3
IntelliJ 版本 14.0.3
regards,
问候,
采纳答案by panagiotis
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).
您必须将您的数据源(首先在数据库视图中创建一个数据源 - 也就是您真正的 JDBC 数据库连接)与持久性单元或会话工厂 (hibernate.cfg.xml) 相关联。
Follow below steps and the warnings will disappear:
按照以下步骤操作,警告将消失:
- Open the Persistence tool window (View | Tool Windows | Persistence).
- Right-click the necessary module, persistence unit or session factory, and select Assign Data Sources in the context menu.
- 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
请参阅此处:将数据源与会话工厂和持久性单元相关联
回答by Mehmood Arbaz
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.
看起来你没有收到任何其他文本的错误,比如@Column
,@GeneratedValue
等等。这意味着使用这些东西的 jars 在你的构建路径中。所以我认为你可以忽略这些类型的错误。
回答by Steven J Owens
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:
我发现 Panos 的回答很有用,但我没有看到有人提到将数据库添加为数据源。也许这足以被假设为例行公事,但如果不是,这就是我必须做的:
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).
在“数据库”窗口中,单击绿色+号并选择“数据源/MySQL”(或您正在使用的任何类型的数据源)。
The Data Sources and Driverswindow 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/Databaseand looked at my MySQL entry. At the bottom it had an error message next to Driver:, and also a MySQLlink. I clicked the MySQLlink and IDEA brought up a popup to fetch Connector/J.
IDEA 可能需要花些功夫才能找到 JDBC 驱动程序。有人告诉我,理论上它应该在我正在从事的项目的 maven 构建过程中找到它,但它没有。我重新打开了View/Tool Windows/Database并查看了我的 MySQL 条目。在底部,它在Driver:旁边有一条错误消息,还有一个MySQL链接。我点击了MySQL链接,IDEA 弹出了一个弹出来获取 Connector/J。
Despite the fact that the checkboxes for Auto commitand Auto syncdefaulted 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.
尽管Auto commit和Auto sync的复选框默认为选中状态并且我保持这种状态,但 IDEA 似乎需要轻推一下。或者也许只是需要一段时间而我不耐烦。
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 Persistenceyet, and while sorting that out, at a coworker's suggestion, I also clicked the Synchronizebutton (two arrows in a circle) on Database.
无论如何,首先我在Database 中双击了我的数据库行。这似乎做到了,但我还没有意识到我需要Persistence,在解决这个问题的同时,在同事的建议下,我还单击了Database上的Synchronize按钮(圆圈中的两个箭头)。
You can also right-click on your database in Databaseand select Synchronize.
您也可以在Database 中右键单击您的数据库并选择Synchronize。
It may take a few seconds, but you should see IDEA filling in the database schema under the entry in Database.
这可能需要几秒钟,但您应该会看到 IDEA 在 Database 条目下填充了数据库架构。
Finally I found Panos's answer and fixed Persistence.
最后我找到了 Panos 的答案并修复了Persistence。
Select View/Tool Windows/Persistence
选择视图/工具窗口/持久性
The Persistence window usually appears on the top left.
Persistence 窗口通常出现在左上角。
In the Persistence window, right-click on your project and select Assign Data Sources.
在 Persistence 窗口中,右键单击您的项目并选择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.
IDEA 会弹出一个包含两列的对话框,左列是您的项目,在我的情况下是右列的空单元格。单击空单元格,IDEA 会为您提供一个下拉列表,允许您选择刚刚添加的数据库。
Again, it may take a few seconds for IDEA to finish analyzing the data source and redo all the inspections.
同样,IDEA 可能需要几秒钟的时间来完成对数据源的分析并重做所有检查。
回答by Manabu Tokunaga
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.
如果您使用的是 JPA,那么将 SQL Dialect 设置为 HSQLDB 而不是 MySQL 也很重要。这通常令人困惑,因为感觉 MySQL 是正确的方言,但实际上您正在处理的 hibernate 是 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.
如果您刚刚升级到 IntelliJ 2017.13 或该版本的其他衍生版本(例如,Webstorm),或者您刚刚重新导入了您的项目,则可能会导致语言注入混乱,需要手动更正。
This can be accomplished via the Project
这可以通过项目完成
Settings -> Languages and Frameworks
设置 -> 语言和框架
part of the IDE.
IDE 的一部分。
回答by yetsun
For those who just want to disable this check, go to
Intellij IDEA -> Preferences -> Search "Unresolved database references in annotations" and uncheck it.
(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.
对于那些只想禁用此检查的人,请转到 Intellij IDEA -> 首选项 -> 搜索“注释中未解析的数据库引用”并取消选中它。
(macOS Mojave 和 Intellij Ultimate 2019.3)
这将禁用检查并删除@Column
注释上的所有“无法解析列...”错误。Intellij 将停止检查数据库表中是否存在字符串列名称。风险自负。
回答by DavidMIRV
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.
仅适用于这没有解决并通过谷歌遇到的任何其他人(如我自己)..通过@Table Annotation设置表名为我修复了它。