tmaolin
tmaolin

Reputation: 21

When I open a querydsl JPA project with vscode, vscode has many cannot be resolved errors

When I open a querydsl JPA project with vscode, vscode has many cannot be resolved errors:

The import com.**.QRoutePayload cannot be resolved

When I open it with IDEA, it's all right. Why do I get this errors in vscode and how can I fix it?

Upvotes: 2

Views: 1067

Answers (2)

Myat Min
Myat Min

Reputation: 1

In order to successfully generate Q classes in VSCode.

Remove the apt-maven-plugin from the POM.

Add following dependencies in the POM.

Please make sure add the version and classifier in the querydsl-apt dependency with provided scope.

<dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-apt</artifactId>
    <version>${querydsl.version}</version>
    <classifier>jpa</classifier>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-jpa</artifactId>
    <version>${querydsl.version}</version>
</dependency>

Upvotes: 0

Yannick
Yannick

Reputation: 683

QueryDSL generates code like the Q-classes (e.g. in your example "QRoutePayload") into your maven target folder. IDEA adds the folder of the generated sources automatically but vscode does not.

So the solution is to add the folder of generated sources to your class path. For example with a maven project you can do it with the build-helper-maven-plugin in your <build>:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>3.2.0</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>target/generated-sources/java</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

You can see what paths are in your class path in vs code by using Ctrl+Shift+P > "Java: Configure Classpath". But if the project ist managed by maven this is read only and you have to use the pom as described above.

Upvotes: 1

Related Questions