简体   繁体   中英

Spring Data JPA QueryDSL

I'm trying to create a controller that uses Spring Data QueryDSL.

My controller looks like this:

   @GetMapping("/playerAccount")
    public Iterable<PlayerAccount> getPlayerAccounts(com.querydsl.core.types.Predicate predicate) {
        return repository.findAll(predicate);
    }

and my entity is (simplified for brevity)

@Entity
public class PlayerAccount {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
}

When I call the GET /playerAccount API, I get the exception IllegalStateException "No primary or default constructor found for interface com.querydsl.core.types.Predicate" (thrown by org.springframework.web.method.annotation.ModelAttributeMethodProcessor#createAttribute).

What could be wrong ?

Thanks

Edit:

my pom.xml contains

<dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-apt</artifactId>
</dependency>
<dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-jpa</artifactId>
</dependency>

and

<plugin>
    <groupId>com.mysema.maven</groupId>
    <artifactId>apt-maven-plugin</artifactId>
    <version>1.1.3</version>
    <executions>
        <execution>
            <goals>
                <goal>process</goal>
            </goals>
            <configuration>
                <outputDirectory>target/generated-sources</outputDirectory>
                <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
            </configuration>
        </execution>
    </executions>
</plugin>

I do not have @EnableSpringDataWebSupport but I must have an equivalent.

You have to keep in mind a few things before make it to work.

  1. Specify below dependencies for query dsl support
<dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-apt</artifactId>
</dependency>
<dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-jpa</artifactId>
</dependency>
  1. Add maven plugin for query class generation (in your case com.fully.qualified.package.name.QPlayerAccount will get generated)
<plugin>
    <groupId>com.mysema.maven</groupId>
    <artifactId>apt-maven-plugin</artifactId>
    <version>1.1.3</version>
    <executions>
        <execution>
            <goals>
                <goal>process</goal>
            </goals>
            <configuration>
                <outputDirectory>target/generated-sources</outputDirectory>
                <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
            </configuration>
        </execution>
    </executions>
</plugin>
  1. Enable Spring data web support by specifying @EnableSpringDataWebSupport to your application's main class to support querydsl from web requests.

  2. Provide meta information for Pradicate to get populated on the go

@GetMapping("/playerAccount")
public Iterable<PlayerAccount> getPlayerAccounts(@QuerydslPredicate(root = PlayerAccount.class) Predicate predicate) {
    return repository.findAll(predicate);
}

Now you are good to go to make request to use query dsl: GET /playerAccount should return all the records And while making call to GET /playerAccount?name=Nick

[
    {
        id: 1,
        name: "Nick"
    }
]

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM