Spring Boot QueryDsl 返回 Caused by: java.lang.UnsupportedOperationException: null

ume*_*esh 7 java jpa spring-mvc spring-data spring-boot

代码如下

    QContinent continent = QContinent.continent;

    JPAQuery query = new JPAQuery(entityManager);


    query.from(continent).where(continent.name.eq("www"));
    List<Object> fetch = query.fetch();

    System.err.println("===" + fetch);
Run Code Online (Sandbox Code Playgroud)

这将返回
Caused by: java.lang.UnsupportedOperationException: null at java.util.Collections$UnmodifyingMap.put(Collections.java:1457) ~[na:1.8.0_191] at com.querydsl.jpa.JPQLSerializer.visitConstant(JPQLSerializer. java:327) ~[querydsl-jpa-4.2.1.jar:na] 在 com.querydsl.core.support.SerializerBase.visit(SerializerBase.java:221) ~[querydsl-core-4.3.1.jar:na ] 在 com.querydsl.core.support.SerializerBase.visit(SerializerBase.java:36) ~[querydsl-core-4.3.1.jar:na] 在 com.querydsl.core.types.ConstantImpl.accept(ConstantImpl.java :140) ~[querydsl-core-4.3.1.jar:na]

Cof*_*Pro 5

正如@user3388770所建议的,原因是版本不匹配。一般来说,在你的pom.xml/中build.gradle不要指定 Spring 已经带来的依赖版本,除非你出于某种原因确实需要它。

您可以在这里找到使用/兼容的依赖项:https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/appendix-dependency-versions.html(根据您的版本更改版本)春季版)

如果出现错误,您的依赖项应如下所示 ( build.gradle):

plugins {
    id "org.springframework.boot" version "2.3.1.RELEASE"
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    ...
}

apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
...

dependencies {
   annotationProcessor(
            ...
            //some put a version below before ":jpa"; dont.
            "com.querydsl:querydsl-apt::jpa"
            ...
    )

    //just an example without version numbers as they are delivered with spring boot automatically
    compile "org.springframework.boot:spring-boot-starter-web"
    compile "org.springframework.boot:spring-boot-actuator"
    ...
    compile group: 'org.apache.httpcomponents', name: 'httpclient'
    compile 'org.thymeleaf.extras:thymeleaf-extras-java8time'

    //but most importantly this below 
    compile "com.querydsl:querydsl-jpa"
}

Run Code Online (Sandbox Code Playgroud)