Intellij IDEA:为JPA验证指定数据源

时间:2022-09-11 12:59:01

I have a Spring project for a small web app set up in Intellij IDEA.

我有一个Spring项目,用于在Intellij IDEA中设置的小型Web应用程序。

It uses JPA on top of Hibernate for the persistence layer. The datasource (MySQL) is defined in Spring application context :

它在Hibernate之上使用JPA作为持久层。数据源(MySQL)在Spring应用程序上下文中定义:

    <!-- Values are configured via the property override -->
    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" >
        <property name="driverClassName" value=""/>
        <property name="url" value=""/>
        <property name="username" value=""/>
        <property name="password" value=""/>
    </bean>

The actual value are read from a properties file and injected at runtime by Spring using the property-override mechanism.

实际值从属性文件中读取,并在Spring运行时使用属性覆盖机制注入。

And then the datasource is injected into the entity manager factory in the same application context:

然后将数据源注入到同一应用程序上下文中的实体管理器工厂中:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
    </bean>

Finally the entity manager injected into the DAOs using an annotation:

最后,实体管理器使用注释注入DAO:

/**
 * Shared, thread-safe proxy for the actual transactional EntityManager
 */
@PersistenceContext
private EntityManager em;

It all works fine when I build and deploy it to Tomcat, but Intellij's JPA validation doesn't seem to understand where to get the datasource from.

当我构建并将其部署到Tomcat时,一切正常,但Intellij的JPA验证似乎并不了解从何处获取数据源。

In my entities, the tables' names and columns' names are underlined in red and the validation message is "cannot resolve table" or "cannot resolve column":

在我的实体中,表的名称和列的名称用红色下划线,验证消息是“无法解析表”或“无法解析列”:

@Entity
@Table(name = "domain")
public class Domain extends AbstractAgendaEntity {

Int this example, it's the "domain" part that is not considered valid.

在此示例中,它是“域”部分,不被视为有效。

I have manually configured my database in the "Database" tool window, I can see my tables and perform SQL queries in the console.

我已经在“数据库”工具窗口中手动配置了我的数据库,我可以在控制台中看到我的表并执行SQL查询。

How can I tell Intellij to use this datasource to resolve table names for my JPA entities?

如何告诉Intellij使用此数据源来解析JPA实体的表名?

7 个解决方案

#1


91  

I finally found out how to do this.

我终于找到了如何做到这一点。

The key is the "persistence" tool window. Apparently it is made available after you add the JPA facet, but is a separate tool window.

关键是“持久性”工具窗口。显然,在添加JPA构面后它可用,但它是一个单独的工具窗口。

To open it : menu "view" -> Tool Windows -> Persistence

要打开它:菜单“查看” - >工具窗口 - >持久性

In this window you see your application with the different persistence related elements (I see persistence.xml, entityManagerFactory from Spring context, and myUnit which I don't know where it comes from.

在这个窗口中,您可以看到具有不同持久性相关元素的应用程序(我看到了persistence.xml,来自Spring上下文的entityManagerFactory,以及我不知道它来自何处的myUnit)。

Here you can right-click on any element and choose "Assign data source". Intellij IDEA:为JPA验证指定数据源

在这里,您可以右键单击任何元素,然后选择“分配数据源”。

This opens a pop-up dialog with a small table containing you persistence elements on the left column and the data source assigned to it on the right column. You can assign a datasource from the "Database" window in there, so I picked the datasource I had configured for my MySQL DB and voilà, the validation errors went away.

这将打开一个弹出对话框,其中包含一个小表,其中包含左列中的持久性元素和右列上分配给它的数据源。你可以从那里的“数据库”窗口分配一个数据源,所以我选择了我为我的MySQL数据库配置的数据源,并且验证错误消失了。

But if I enter a wrong table or column name, I still get an error, which is pretty neat.

但是,如果我输入错误的表或列名称,我仍然会收到错误,这非常简洁。

#2


12  

First thing you have to add data source into your IDE. You can do it in the tab "Database" usually on right side. You can import this data source from your code. You should make sure that you hit button refresh tables. IDEA will load tables and use them for validation. Then you have to inside your JPA facet setup this data source.

首先要将数据源添加到IDE中。您可以通常在右侧的“数据库”选项卡中执行此操作。您可以从代码中导入此数据源。您应该确保按下按钮刷新表。 IDEA将加载表并使用它们进行验证。然后,您必须在JPA facet内部设置此数据源。

#3


5  

There are a few things you need to do. First, configure a Hibernate facet in your Project Structure configuration. You can select your Hibernate configuration file at this point or create a new one. You should then configure your data sources in the Database window (View->Tools Window->Database). Remember to set the database dialect on the Console tab in the database window. Finally, you need to go to the Persistence window (View->Tools Window->Persistence) and add a data source to the appropriate facet. Just right click on the right icon in the tree and select "Add Datasource". The Data Source column has a drop down menu containing all the data sources you have configured. IntelliJ then correctly identifies the tables.

您需要做一些事情。首先,在Project Structure配置中配置Hibernate方面。您可以在此时选择Hibernate配置文件或创建一个新文件。然后,您应该在数据库窗口中配置数据源(视图 - >工具窗口 - >数据库)。请记住在数据库窗口的“控制台”选项卡上设置数据库方言。最后,您需要转到Persistence窗口(View-> Tools Window-> Persistence)并将数据源添加到相应的facet。只需右键单击树中的右侧图标,然后选择“添加数据源”。 “数据源”列有一个下拉菜单,其中包含您已配置的所有数据源。 IntelliJ然后正确识别表。

One word of warning. As of v12.04, IntelliJ does not modify your Hibernate configuration file. You still need to map your classes and manually add your database details.

一句警告。从v12.04开始,IntelliJ不会修改您的Hibernate配置文件。您仍需要映射类并手动添加数据库详细信息。

#4


1  

On top of setting a data source like others have mentioned, in order to clear these false errors, in the Database panel, I had to go to settings, Schemas & Tables tab, and hit the checkbox left of the "information_schema" table for my database.

除了像其他人提到的那样设置数据源之外,为了清除这些错误错误,在数据库面板中,我必须转到设置,模式和表格选项卡,然后点击“information_schema”表格左侧的复选框。数据库。

#5


0  

Make sure you are importing javax.persistence.Table and javax.persistence.Column within your entity classes.

确保在实体类中导入javax.persistence.Table和javax.persistence.Column。

So at the top of each class check for:

因此,在每个班级的顶部检查:

import javax.persistence.Table;
import javax.persistence.Column;

This will import the proper JPA annotations into your class.

这会将适当的JPA注释导入到您的班级中。

#6


0  

I had the data source set correctly, but the column names were not shown. After I changed the schema and the catalog like below, everything was recognized correctly.

我正确设置了数据源,但未显示列名。在我更改了下面的架构和目录后,所有内容都被正确识别。

@Entity
@Table(name = "stock_detail", schema = "testing", catalog = "")
public class Xyz { 
    // ...

#7


0  

For JPA cannot resolve table/ column. If everything works and you just annoyed by the red error mark, you can change the inspection settings from Error to Warning:-

对于JPA无法解析表/列。如果一切正常并且您只是被红色错误标记所困扰,您可以将检查设置从错误更改为警告: -

File --> Settings --> Inspections --> JPA issues --> Unresolved database references in annotations.

文件 - >设置 - >检查 - > JPA问题 - >注释中未解析的数据库引用。

#1


91  

I finally found out how to do this.

我终于找到了如何做到这一点。

The key is the "persistence" tool window. Apparently it is made available after you add the JPA facet, but is a separate tool window.

关键是“持久性”工具窗口。显然,在添加JPA构面后它可用,但它是一个单独的工具窗口。

To open it : menu "view" -> Tool Windows -> Persistence

要打开它:菜单“查看” - >工具窗口 - >持久性

In this window you see your application with the different persistence related elements (I see persistence.xml, entityManagerFactory from Spring context, and myUnit which I don't know where it comes from.

在这个窗口中,您可以看到具有不同持久性相关元素的应用程序(我看到了persistence.xml,来自Spring上下文的entityManagerFactory,以及我不知道它来自何处的myUnit)。

Here you can right-click on any element and choose "Assign data source". Intellij IDEA:为JPA验证指定数据源

在这里,您可以右键单击任何元素,然后选择“分配数据源”。

This opens a pop-up dialog with a small table containing you persistence elements on the left column and the data source assigned to it on the right column. You can assign a datasource from the "Database" window in there, so I picked the datasource I had configured for my MySQL DB and voilà, the validation errors went away.

这将打开一个弹出对话框,其中包含一个小表,其中包含左列中的持久性元素和右列上分配给它的数据源。你可以从那里的“数据库”窗口分配一个数据源,所以我选择了我为我的MySQL数据库配置的数据源,并且验证错误消失了。

But if I enter a wrong table or column name, I still get an error, which is pretty neat.

但是,如果我输入错误的表或列名称,我仍然会收到错误,这非常简洁。

#2


12  

First thing you have to add data source into your IDE. You can do it in the tab "Database" usually on right side. You can import this data source from your code. You should make sure that you hit button refresh tables. IDEA will load tables and use them for validation. Then you have to inside your JPA facet setup this data source.

首先要将数据源添加到IDE中。您可以通常在右侧的“数据库”选项卡中执行此操作。您可以从代码中导入此数据源。您应该确保按下按钮刷新表。 IDEA将加载表并使用它们进行验证。然后,您必须在JPA facet内部设置此数据源。

#3


5  

There are a few things you need to do. First, configure a Hibernate facet in your Project Structure configuration. You can select your Hibernate configuration file at this point or create a new one. You should then configure your data sources in the Database window (View->Tools Window->Database). Remember to set the database dialect on the Console tab in the database window. Finally, you need to go to the Persistence window (View->Tools Window->Persistence) and add a data source to the appropriate facet. Just right click on the right icon in the tree and select "Add Datasource". The Data Source column has a drop down menu containing all the data sources you have configured. IntelliJ then correctly identifies the tables.

您需要做一些事情。首先,在Project Structure配置中配置Hibernate方面。您可以在此时选择Hibernate配置文件或创建一个新文件。然后,您应该在数据库窗口中配置数据源(视图 - >工具窗口 - >数据库)。请记住在数据库窗口的“控制台”选项卡上设置数据库方言。最后,您需要转到Persistence窗口(View-> Tools Window-> Persistence)并将数据源添加到相应的facet。只需右键单击树中的右侧图标,然后选择“添加数据源”。 “数据源”列有一个下拉菜单,其中包含您已配置的所有数据源。 IntelliJ然后正确识别表。

One word of warning. As of v12.04, IntelliJ does not modify your Hibernate configuration file. You still need to map your classes and manually add your database details.

一句警告。从v12.04开始,IntelliJ不会修改您的Hibernate配置文件。您仍需要映射类并手动添加数据库详细信息。

#4


1  

On top of setting a data source like others have mentioned, in order to clear these false errors, in the Database panel, I had to go to settings, Schemas & Tables tab, and hit the checkbox left of the "information_schema" table for my database.

除了像其他人提到的那样设置数据源之外,为了清除这些错误错误,在数据库面板中,我必须转到设置,模式和表格选项卡,然后点击“information_schema”表格左侧的复选框。数据库。

#5


0  

Make sure you are importing javax.persistence.Table and javax.persistence.Column within your entity classes.

确保在实体类中导入javax.persistence.Table和javax.persistence.Column。

So at the top of each class check for:

因此,在每个班级的顶部检查:

import javax.persistence.Table;
import javax.persistence.Column;

This will import the proper JPA annotations into your class.

这会将适当的JPA注释导入到您的班级中。

#6


0  

I had the data source set correctly, but the column names were not shown. After I changed the schema and the catalog like below, everything was recognized correctly.

我正确设置了数据源,但未显示列名。在我更改了下面的架构和目录后,所有内容都被正确识别。

@Entity
@Table(name = "stock_detail", schema = "testing", catalog = "")
public class Xyz { 
    // ...

#7


0  

For JPA cannot resolve table/ column. If everything works and you just annoyed by the red error mark, you can change the inspection settings from Error to Warning:-

对于JPA无法解析表/列。如果一切正常并且您只是被红色错误标记所困扰,您可以将检查设置从错误更改为警告: -

File --> Settings --> Inspections --> JPA issues --> Unresolved database references in annotations.

文件 - >设置 - >检查 - > JPA问题 - >注释中未解析的数据库引用。