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

Viewed 96

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?

1 Answers

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.

Related