Intellij IDEA: specify datasource for JPA validation Intellij IDEA: specify datasource for JPA validation spring spring

Intellij IDEA: specify datasource for JPA validation


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.

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.

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

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.


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.


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.

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.