JpaRepository Querydsl : Qclass not generates

梦想与她 提交于 2019-12-11 01:23:04

问题


I find that there is many question like mine but never had the answer to enable Querydsl in my Spring-boot project. this is my pom.xml dependency and plugin:

    <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>

and this is my Entity:

@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;
...

and this is the repository:

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

}

my project do not run and it is error says Did not find a query class ir.mtajik.entities.QPhone for domain class ir.mtajik.entities.Phone and cause by :can not create QPhone class. I googled so much. there are lots of question like mine but answers are not working in my situation. I tried these:

  1. chose dependency and plugin from same version
  2. Try to use @Document but it not working and do not know where to put that.
  3. I tried many version of querydsl but the error is same

回答1:


It worked for me when I changed the processor.

So you have it as:

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

Change it to :

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

Hope this helps ;)




回答2:


as mentioned in comments along with that you also need to configure Maven-apt plugin for Query DSL, so add this in pom Plugins section:

<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>

Hope this helps. This Spring Article will help you. And Don't forget to mvn clean install

Also make sure, Beans are defined either XML based or Java Based for EntityManager, Jpa Vendor, and pointing to your Repo package.




回答3:


Try to use this dependencies. It works fine for me.

<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>


来源:https://stackoverflow.com/questions/42255734/jparepository-querydsl-qclass-not-generates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!