51

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

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>

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>

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

/**
 * 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.

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.

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

Betlista
  • 10,327
  • 13
  • 69
  • 110
Pierre Henry
  • 16,658
  • 22
  • 85
  • 105

9 Answers9

136

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.

Pipo
  • 4,653
  • 38
  • 47
Pierre Henry
  • 16,658
  • 22
  • 85
  • 105
  • 12
    For those who are as ignorant as I... You can get to the persistence tool window by selecting: View | Tool Windows | Persistence. – kc2001 Feb 07 '13 at 20:21
  • For IDEA 10, window is found in: Window | Tool Windows | Persistence. – Jamie Burke Apr 10 '14 at 09:40
  • 1
    Great. Finally an IDE that does what it's supposed to do (instead of hanging, having buggy base functionality etc.) An you can easily configure it to have E****** keymaps to transition smoothly from your corporal environment. – Kawu Apr 11 '14 at 07:37
  • This almost did it for me, except I also had to go in to Facets -> JPA -> Data Sources Mapping and map the Persistence Unit to the data source. – martingreber May 06 '14 at 03:33
  • 1
    I had to add a Spring facet and manually add my @Entity class to the Spring Application Context before I saw an entry in the "Assign Data Sources" window. – Gillfish Jan 25 '16 at 21:38
15

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.

chalimartines
  • 5,603
  • 2
  • 23
  • 33
6

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.

David Weber
  • 1,965
  • 1
  • 22
  • 32
3

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

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

justMe
  • 2,200
  • 16
  • 20
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.

Alkanshel
  • 4,198
  • 1
  • 35
  • 54
  • That was the crucial additional information I needed in IDEA 15.0.4 – Mark Mar 02 '16 at 09:53
  • On intellij 2017 now. "Refresh tables" isn't working anymore, nor is this answer.. I think intellij just has bugs in this regard. It doesn't recognize my table name / unique key column names no matter what I do. – Alkanshel Feb 07 '18 at 00:30
0

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

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.

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • You misunderstood the problem. The annotation is recognized and the code compiles, but the extra JPA validation provided by Intellij cannot find the table in the MySQL database. – Pierre Henry Jan 16 '13 at 10:29
  • @PierreHenry Sorry about that. Thanks for the clarification. Are you specifying a name element on the table and column annotation? If not they may not be mapping correctly depending on the casing of your entity/table names – Kevin Bowersox Jan 16 '13 at 10:30
  • Yes I have a @Table with a name (I edited the question with a sample entity mapping.) The name itself is not the problem since when I run the app it works. The problem is Intellij doesn't know where to look for the table... – Pierre Henry Jan 16 '13 at 10:35
  • @PierreHenry have you setup the project to be faceted as JPA? – Kevin Bowersox Jan 16 '13 at 10:46
  • Yes. It has a JPA facet, and the descriptor points at my persistence.xml in the resources/META-INF folder, but this file doesn't contain datasource parameters. – Pierre Henry Jan 16 '13 at 10:56
  • Sorry I couldn't be of more help. Here is a similar issue: http://stackoverflow.com/questions/12420996/intellij-idea-highlights-entity-class-names-with-cannot-resolve-symbol-in-jp – Kevin Bowersox Jan 16 '13 at 10:59
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 { 
    // ...
Martin Pfeffer
  • 12,471
  • 9
  • 59
  • 68
0

As the path is different depending on your idea version. So from File --> Settings just search for : Unresolved database references. Then do whatever seems ok to you : uncheck the validation or change severity flag.

Breton F.
  • 177
  • 1
  • 6
0

Keep mouse cursor upon the table name for a while and context menu will appear where you can assign datasource.

Simon Logic
  • 330
  • 4
  • 10