52

IntelliJ IDEA highlights persistent @Entity class names with "Cannot resolve symbol" in red in JPQL which is distracting and buries real issues.

So, for example, I declare a query in my repository:

private static final String READ_BY_CANDIDATE_KEY = "SELECT cr FROM Entity AS cr left join cr.relationship AS re left join fetch cr.relationship2 WHERE re.candidateKey=:ID";

.. and "Entity" is underlined, even though "Entity" is a valid class name, and has the @Entity annotation. When the code actually runs, there are no problems.

I imagine some sort of configuration is required to let the IDE know what classes are valid? How is that configuration done?

Update: I do have a JPA facet, but it doesn't see the annotated classes. It seems to require a persistence.xml or orm.xml (which my project does not use)

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Simon Gibbs
  • 4,737
  • 6
  • 50
  • 80

9 Answers9

60

Seems like you have not selected the default JPA provider in facet configuration. Depending upon which provider you are using, pick one from the list. Available options are EclipseLink, Hibernate, OpenJPA, TopLink

rajnish
  • 809
  • 8
  • 5
25

Make sure you have JPA or Hibernate facet configured in IntelliJ IDEA for your module.

Prashant
  • 4,775
  • 3
  • 28
  • 47
CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • 1
    Are you using Spring? If so, Spring facet is required. – CrazyCoder Sep 18 '12 at 13:43
  • could you provide instructions on how to do the above please? – JonnyRaa Nov 22 '13 at 11:18
  • 4
    If you are using Spring for your JPA config (no persistence.xml) just create a JPA facet and set the default JPA Provider. No xml file needs to be specified. – Dave Feb 11 '15 at 21:20
  • You also have to right-click the root item in the Persistence menu and hit Assign Data Sources... then pick your DB and you should be all set – Alkanshel Sep 14 '15 at 21:40
  • 2
    @Amalgovinus For Spring Boot applications there is nothing to choose from. Even when you create JPA and/or Hibernate facets manually, list stays empty. – wst Aug 26 '16 at 14:16
4

If you are using Spring Boot with maven, add this dependency in your pom.xml file.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
user3841581
  • 2,637
  • 11
  • 47
  • 72
3

File -> Project Structure

At left pane select "Facets". If there is no JPA listed, click "+" sign and add "JPA"

At bottom of same dialog, at "Default JPA Provider", select - "Hibernate", press "OK"

If you have error at @Table annotation, configure and choose data source

Table name for select now should be recognized as entity class name

GintsGints
  • 807
  • 7
  • 15
2

you would have missed this dependency- Spring data JPA This one is for Maven projects

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

add this one in your POM under dependencies section and then use ctrl+click on @Entity to import it from

import javax.persistence.Entity;

for Gradle follow the same and use

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

under dependencies in the build.gradle file

  • For gradle, make sure you refresh gradle, so that it rebuilds in Intellij for this to come in effect. – snapshot Mar 05 '23 at 23:48
2

I ran into the same symptom as the OP (IntelliJ highlights entity in JPQL with the error "Cannot resolve symbol") but the solution turned out to be invalidating the IntelliJ caches and restarting the IDE.

kensei62
  • 144
  • 9
0

Try adding this dependency if you are using Spring-boot.
spring-boot-starter-data-jpa

0

Sometimes JPA Buddy plugin brakes the springboot JPA configuration. Make sure this is not causing the issue.

  • I'm the head of JPA Buddy development team. So, I wonder what case you have mentioned? As we have never faced something like this and don't even touch the configuration part... Could you please provide more information, so we could fix the problem if it exists. – aleksey.stukalov Feb 16 '22 at 10:53
0

I had spring-boot-starter-data-jpa dependency in pom.xml correctly, for me this fixed it:

  1. Right click your project -> Maven -> Reload project

enter image description here

This fixed also for me error: "Cannot resolve symbol 'Query'"

tuiz
  • 900
  • 7
  • 4