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)
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
Make sure you have JPA or Hibernate facet configured in IntelliJ IDEA for your module.
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>
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
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
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.
Try adding this dependency if you are using Spring-boot.
spring-boot-starter-data-jpa
Sometimes JPA Buddy plugin brakes the springboot JPA configuration. Make sure this is not causing the issue.
Related
We are developing Spring Boot application that is using MongoDB as storage.
And we want to add to our project the DB migration tool: mongock.
in pom.xml I added a new dependency:
<dependency>
<groupId>com.github.cloudyrock.mongock</groupId>
<artifactId>mongock-spring</artifactId>
<version>3.3.2</version>
</dependency>
And IntelliJ Idea advised me to add the following lines to module-info.java:
requires mongock.spring;
requires mongock.core;
After that I am not able anymore to build the project, I am getting the following error:
Module 'com.acme.project-name' reads package 'com.github.cloudyrock.mongock' from both 'mongock.spring' and 'mongock.core'
I do not know a lot about Java 9 Modularity, that why I am stuck with resolving this issue, please advice.
If it's worth solving this issue one could upgrade to the latest release of the artifact.
<dependency>
<groupId>com.github.cloudyrock.mongock</groupId>
<artifactId>mongock-spring</artifactId>
<version>4.0.1.alpha</version>
</dependency>
You can understand what the issue means over this and this Q&A.
If you were to analyze the issue, you could straightforward notice that with the version 3.3.2, there are two artifacts that are brought in as a dependency under external libraries - mongock-spring and mongock-core. Further, if you look at the JARs, you would see their package structure is the same( i.e. both have classes within com.github.cloudyrock.mongock) and that is the reason for conflict that you see while they both are introduced in the modulepath.
Edit: Extended discussions to be moved over to #mongock/issues/212.
Created a Maven Project and have set the dependencies in the pom.xml. The import statement (com.splunk.*) seems to recognize the library included in the Maven Dependencies, however when I refer to the class (for e.g. ResultsReaderXml) within that library, it shows the "error message cannot be resolved to type". Can some please explain why Spring Tool Suite (Eclipse) is doing that?
https://i.stack.imgur.com/enCRW.png
After adding the following dependency to pom.xml
<dependency>
<groupId>com.timgroup</groupId>
<artifactId>java-statsd-client</artifactId>
<version>3.1.0</version>
</dependency>
Eclipse complains:
Overriding managed version 3.1.0 for java-statsd-client
How would I find out where (in which existing dependency) is that overriden version resides?
Tried the suggestion by #Evan LaHurd - here's what I'm getting in Eclipse:
Thanks.
To answer your question about how to see the parent pom.xml within Eclipse:
Open the POM file and navigate to the overview tab in the editor. There is a small icon with the tooltip "Open Parent POM" in the Parent section (it looks like a little sheet with an M on it and a yellow arrow).
From here you can look at the dependency management section like Tunaki said either in the Dependencies tab or directly in the pom.xml file.
I have a bean class with a property setter like this:
public void setDueDate(java.sql.Date dueDate)
I also have an instance of this bean configured in XML like this:
<property name="dueDate">
<bean class="java.sql.Date"/>
</property>
STS marks that config with an error:
No constructor with 0 arguments defined in class 'java.sql.Date'
Well, that's true, java.sql.Date has no no-arg constructor. But this app works fine so obviously Spring is smart enough to create a Date instance without a constructor. Question is, why is the STS editor/builder complaining, and is it possible to convince it that this is not an error or warning?
At this time, I can only see 3 situations where the code "would work", in the order of their likeliness to happen:
the parent bean where the property is injected is defined with scope="prototype" or lazy-init="true" and is not accessed at all
the runtime classpath contains an implementation of java.sql.Dateshadowing the original
that particular context xml is not used in the application
Otherwise, Spring should issue a nice:
Could not instantiate bean class [java.sql.Date]: No default constructor found; nested exception is java.lang.NoSuchMethodException: java.sql.Date.<init>()
In case the code works, but STS reports an wrong error, please file a bug against STS at https://issuetracker.springsource.com/browse/STS (as mentioned above).
In the meantime you can workaround this issue by disabling the validation via the Spring preferences for the project. Just go to preferences of the project -> Spring, then the "Project Validators" tab, and deactivate the validation that is responsible for the wrong error report.
I would like to use JPA annotations in ormlite. These annotations (like #Entity or #Id) belong to the javax.persistence package which is apparently not provided with ormlite. I could not get JPA annotations resolved, whereas ormlite specific annotations (like #DatabaseTable) are ok. Do I need to download a third party jar in order to get JPA annotations working in ormlite ?
I need to work with ormlite + JPA only.
When having a look at http://ormlite.com/javadoc/ormlite-core/index-all.html we can see that there are no JPA annotations documented nor available, although they are described in the user manual.
Thanks a lot in advance !!!
javax.persistence is available in a jar from the central maven repository (for example). It contains all annotations as well as all of the other JPA stuff that ORMLite ignores.
CMobileCom JPA is a new implementation of JPA specification for both Java JDBC and Android. It is light-weight, about 380K. You can add the following gradle dependencies:
Android:
dependencies {
implementation("com.cmobilecom:cmobilecom-jpa-android:${version}#aar") {
transitive = true
}
annotationProcessor "com.cmobilecom:cmobilecom-jpa-processor:$version"
}
Java JDBC:
dependencies {
implementation "com.cmobilecom:cmobilecom-jpa-jdbc:$version"
// annotation processor: generate static metamodel
compileOnly "com.cmobilecom:cmobilecom-jpa-processor:$version"
}
JPA annotations are supported, so you do not need to add another jar to resolve JPA annotations in your code.
See Developer Guide:
https://cmobilecom.com/docs/jpa/latest/devguide/index.html
Disclaimer: I am a developer of CMobileCom JPA.