Intellij IDEA:为JPA验证指定数据源 [英] Intellij IDEA: specify datasource for JPA validation

查看:2101
本文介绍了Intellij IDEA:为JPA验证指定数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

    <!-- 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>

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

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

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

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>

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

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

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

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

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.

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

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 {

在这个例子中,它是domain部分不被认为是有效的。

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

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

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

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

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

推荐答案

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

I finally found out how to do this.

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

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

要打开它:菜单视图 - >工具窗口 - >持久性

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

在此窗口中,您将看到具有不同持久性相关元素的应用程序(我看到 persistence.xml ,<$ c来自Spring上下文的$ c> entityManagerFactory ,以及 myUnit ,我不知道它来自哪里。

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.

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

Here you can right-click on any element and choose "Assign data source".

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

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.

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

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

这篇关于Intellij IDEA:为JPA验证指定数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆