Simon Gibbs
Simon Gibbs

Reputation: 4818

IntelliJ IDEA highlights @Entity class names with "Cannot resolve symbol" in JPQL

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)

Upvotes: 53

Views: 83984

Answers (10)

Atulit Anand
Atulit Anand

Reputation: 16

Here is the solution that worked for me,

Right click on project folder > At the very bottom click maven > Click Download Sources or Download Sources and Documentation.

Picuture of intellij ide showing the download sources option under maven

If similar issues happens in eclipse IDE, it can be resolved by doing

Right Click on project folder > Maven > Download Sources and Download JavaDoc > Then hover over the error and it will show and option of organise project imports click on it and then save the project using Ctrl + s

If someone knows a cleaner way to do this in eclipse, kindly share.

Upvotes: 0

tuiz
tuiz

Reputation: 948

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'"

Upvotes: 0

kensei62
kensei62

Reputation: 164

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.

Upvotes: 3

Vignesh Ravichandran
Vignesh Ravichandran

Reputation: 189

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

Upvotes: 2

Mike Ballesteros
Mike Ballesteros

Reputation: 11

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

Upvotes: 0

user3841581
user3841581

Reputation: 2747

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>

Upvotes: 4

Anirudh Khanna
Anirudh Khanna

Reputation: 99

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

Upvotes: 0

GintsGints
GintsGints

Reputation: 853

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

Upvotes: 4

CrazyCoder
CrazyCoder

Reputation: 402393

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

Upvotes: 26

rajnish
rajnish

Reputation: 829

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

Upvotes: 62

Related Questions