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

查看:34
本文介绍了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?

推荐答案

我终于找到了方法.

关键是持久化"工具窗口.显然,它在您添加 JPA facet 后可用,但它是一个单独的工具窗口.

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

在此窗口中,您会看到具有不同持久性相关元素的应用程序(我从 Spring 上下文中看到 persistence.xmlentityManagerFactorymyUnit> 我不知道它来自哪里.

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 数据库配置的数据源,瞧,验证错误消失了.

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天全站免登陆