JPA: QUery dsl error: java.lang.NoSuchFieldError: LIKE_IC

JPA: QUery dsl error: java.lang.NoSuchFieldError: LIKE_IC

虽然 运行 查询 dsl 我遇到了以下错误。

    018-05-01 13:18:38.517 DEBUG 372 --- [nio-8080-exec-1] o.s.b.c.web.OrderedRequestContextFilter  : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@308cc4e8
    2018-05-01 13:18:38.520 ERROR 372 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [/boot] threw exception [Handler processing failed; nested exception is java.lang.NoSuchFieldError: LIKE_IC] with root cause

    java.lang.NoSuchFieldError: LIKE_IC
        at com.mysema.query.jpa.JPQLTemplates.<clinit>(JPQLTemplates.java:38) ~[querydsl-jpa-3.7.4.jar:na]

当我再次点击同一个请求时,我得到了不同的异常:

2018-05-01 13:36:47.839 DEBUG 7964 --- [nio-8080-exec-3] o.s.web.servlet.DispatcherServlet        : Could not complete request

org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.mysema.query.jpa.HQLTemplates
    at org.springframework.web.servlet.DispatcherServlet.triggerAfterCompletionWithError(DispatcherServlet.java:1305) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:979) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895) ~[spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967) [spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869) [spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) [tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) [spring-webmvc-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) [tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292) [tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) [tomcat-embed-websocket-8.0.33.jar:8.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240) [tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207) [tomcat-embed-core-8.0.33.jar:8.0.33]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) [spring-web-4.2.6.RELEASE.jar:4.2.6.RELEASE]
    at org.springframework.web.filter.OncePerRequ

我的域 class 如下,我正在调用 dsl 搜索查询

package com.springboot.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

@Entity
@Table(name = "app_user", catalog = "testdb")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class AppUser implements java.io.Serializable {

    Integer id;
    private Rank rank;
    private Trainingstatus trainingstatus;
    private String name;
    private int age;
    private double salary;
    Set<Address> addresses = new HashSet<Address>(0);


    public AppUser(Rank rank, Trainingstatus trainingstatus, String name, int age, double salary,
            Set<Address> addresses) {
        this.rank = rank;
        this.trainingstatus = trainingstatus;
        this.name = name;
        this.age = age;
        this.salary = salary;
        this.addresses = addresses;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)

    @Column(name = "id", unique = true, nullable = false)
    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "rankId", nullable = false)
    //@JsonManagedReference(value="rank")

    public Rank getRank() {
        return this.rank;
    }

    public void setRank(Rank rank) {
        this.rank = rank;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "trainingStatusId", nullable = false)
    //@JsonManagedReference(value="trainingstatus")
    /*@JsonBackReference
    @JsonIgnore*/
    public Trainingstatus getTrainingstatus() {
        return this.trainingstatus;
    }

    public void setTrainingstatus(Trainingstatus trainingstatus) {
        this.trainingstatus = trainingstatus;
    }

    @Column(name = "name", nullable = false, length = 30)
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "age", nullable = false)
    public int getAge() {
        return this.age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Column(name = "salary", nullable = false, precision = 22, scale = 0)
    public double getSalary() {
        return this.salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "appUser" , cascade = CascadeType.ALL )
    public Set<Address> getAddresses() {
        return this.addresses;
    }

    public void setAddresses(Set<Address> addresses) {
        this.addresses = addresses;
    }
}

我的 dsl 查询

QAppUser qappUser = QAppUser.appUser;
            BooleanExpression rankExp = qappUser.rank.rank.equalsIgnoreCase(user.getRank().getRank());
            BooleanExpression nameExp = qappUser.name.equalsIgnoreCase(user.getName());
            List<AppUser> createdUser = (List<AppUser>) userRepo.findAll(nameExp.and(rankExp));77

Github 项目-https://github.com/harshalpatil2012/Springboot 正常的 JPA 查询在这里工作而不是 query-dsl 可以在这里帮助我。

您好,已参考此页面并从项目中删除了查询 dsl 核心包依赖项,之后它运行良好。

在 spring 引导中,我们不需要 query-dsl 核心包,这是导致上述错误的原因。

        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-core</artifactId>
            <version>3.6.7</version>
        </dependency>

对于 spring 下面的启动依赖项就足够了。

<dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-apt</artifactId>
            <version>3.7.4</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-jpa</artifactId>
            <version>3.7.4</version>
        </dependency>

<plugin>
                <groupId>com.mysema.maven</groupId>
                <artifactId>apt-maven-plugin</artifactId>
                <version>1.1.2</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>src/main/generated/</outputDirectory>
                            <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>com.mysema.querydsl</groupId>
                        <artifactId>querydsl-apt</artifactId>
                        <version>3.7.4</version>
                    </dependency>
                </dependencies>
            </plugin>