JpaRepository Querydsl:Qclass不生成

时间:2017-02-15 17:02:02

标签: maven spring-boot querydsl

我发现有很多类似我的问题,但从来没有在我的 Spring-boot 项目中启用 Querydsl 的答案。 这是我的pom.xml依赖项和插件:

    <properties>
        <querydsl.version>3.7.4</querydsl.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-core</artifactId>
            <version>${querydsl.version}</version>
        </dependency>

        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-apt</artifactId>
            <version>${querydsl.version}</version>
        </dependency>

        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-jpa</artifactId>
            <version>${querydsl.version}</version>
        </dependency>



    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            //added for QueryDsl
            <plugin>
                <groupId>com.mysema.maven</groupId>
                <artifactId>maven-apt-plugin</artifactId>
                <version>1.0</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>process</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/generated-sources</outputDirectory>
                            <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

        </plugins>
    </build>


</project>

这是我的实体:

@Entity
@Table(name = "phone_table")
public class Phone {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long phoneId;
private String phoneName;
private String phoneBrand;
private String phoneManufacturer;
...

这是存储库:

@Repository
public interface MobileRepository extends JpaRepository<Phone,Long> ,QueryDslPredicateExecutor<Phone>{

}

我的项目没有运行,错误显示Did not find a query class ir.mtajik.entities.QPhone for domain class ir.mtajik.entities.Phone并导致:无法创建QPhone类。我用Google搜索了这么多。像我一样有很多问题,但答案在我的情况下不起作用。 我试过这些:

  1. 从同一版本中选择了依赖项和插件
  2. 尝试使用@Document,但它不起作用,不知道放在哪里。
  3. 我尝试了很多版本的 querydsl ,但错误相同

3 个答案:

答案 0 :(得分:3)

当我更换处理器时,它对我有用。

所以你把它作为:

<configuration>
        <outputDirectory>target/generated-sources</outputDirectory>
        <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>

将其更改为:

<configuration>
         <outputDirectory>target/generated-sources</outputDirectory>
         <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>

希望这会有所帮助;)

答案 1 :(得分:2)

如评论中所述,您还需要为查询DSL配置Maven-apt插件,因此请在pom插件部分添加:

<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/java</outputDirectory>
            <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
        </configuration>
    </execution>
</executions>

希望这会有所帮助。 This Spring Article会帮助你。并且不要忘记 mvn clean install

另外请确保,Bean已定义为基于XML或基于Java的EntityManager,Jpa Vendor,并指向您的Repo包。

答案 2 :(得分:0)

尝试使用此依赖项。对我来说很好。

<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.3</version>
  <executions>
    <execution>
      <goals>
        <goal>process</goal>
      </goals>
      <configuration>
        <outputDirectory>target/generated-sources/java</outputDirectory>
        <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
      </configuration>
    </execution>
  </executions>
</plugin>