2014-04-13 231 views
3

我是Querydsl的新手,嘗試在簡單的測試項目中使用它。 我跟着官方教程來配置我的pom.xml,然後mvn clean install能夠在target/generated-sources/java下生成Q Classes。 但我得到以下錯誤:Querydsl maven編譯錯誤:QClass.class不存在

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project spring-jqgrid-tutorial: Compilation failure 

[ERROR] /D:/Project/spring-jqgrid-tutorial/src/main/java/org/krams/controller/UserController.java:[92,62] package QUser.user does not exist 

我認爲,根本原因是生成的Q類的源文件不會被自動編譯成二進制類文件。我沒有確認我的項目目錄下沒有QUser.class。 我還嘗試使用build-helper-maven-plugin將target/generated-sources/java添加爲源文件夾,並在apt-maven-plugin配置中將target/generated-sources/java指定爲額外的源根目錄。但我沒有運氣。

這裏是我的pom.xml

<properties> 
    <querydsl.version>3.3.2</querydsl.version> 
    <maven.compiler.plugin.version>3.1</maven.compiler.plugin.version> 
    <maven.apt.plugin.version>1.1.1</maven.apt.plugin.version 
    <maven.build.helper.plugin.version>1.8</maven.build.helper.plugin.version> 
<properties> 

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

<plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>${maven.compiler.plugin.version}</version> 
      <configuration> 
       <source>1.7</source> 
       <target>1.7</target> 
      </configuration> 
     </plugin> 

     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>build-helper-maven-plugin</artifactId> 
       <version>${maven.build.helper.plugin.version}</version> 
      <executions> 
       <execution> 
        <id>add-source</id> 
        <phase>process-classes</phase> 
        <goals> 
         <goal>add-source</goal> 
        </goals> 
        <configuration> 
         <sources> 
          <source>target/generated-sources/java</source> 
         </sources> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 

     <plugin> 
      <groupId>com.mysema.maven</groupId> 
      <artifactId>apt-maven-plugin</artifactId> 
      <version>${maven.apt.plugin.version}</version> 
      <executions> 
       <execution> 
        <goals> 
         <goal>process</goal> 
        </goals> 
        <configuration> 
         <outputDirectory>target/generated-sources/java</outputDirectory> 
         <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor> 
         <additionalSourceRoots> 
          <additionalSourceRoot>target/generated-sources/java</additionalSourceRoot> 
         </additionalSourceRoots> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
<plugins> 

這裏是產生QUser.java

package org.krams.domain; 

import static com.mysema.query.types.PathMetadataFactory.*; 

import com.mysema.query.types.path.*; 

import com.mysema.query.types.PathMetadata; 
import javax.annotation.Generated; 
import com.mysema.query.types.Path; 
import com.mysema.query.types.path.PathInits; 


/** 
* QUser is a Querydsl query type for User 
*/ 
@Generated("com.mysema.query.codegen.EntitySerializer") 
public class QUser extends EntityPathBase<User> { 

    private static final long serialVersionUID = -1712499619L; 

    private static final PathInits INITS = PathInits.DIRECT2; 

    public static final QUser user = new QUser("user"); 

    public final NumberPath<Integer> age = createNumber("age", Integer.class); 

    public final StringPath firstName = createString("firstName"); 

    public final NumberPath<Long> id = createNumber("id", Long.class); 

    public final StringPath lastName = createString("lastName"); 

    public final StringPath password = createString("password"); 

    public final QRole role; 

    public final StringPath username = createString("username"); 

    public QUser(String variable) { 
     this(User.class, forVariable(variable), INITS); 
    } 

    public QUser(Path<? extends User> path) { 
     this(path.getType(), path.getMetadata(), path.getMetadata().isRoot() ? INITS : PathInits.DEFAULT); 
    } 

    public QUser(PathMetadata<?> metadata) { 
     this(metadata, metadata.isRoot() ? INITS : PathInits.DEFAULT); 
    } 

    public QUser(PathMetadata<?> metadata, PathInits inits) { 
     this(User.class, metadata, inits); 
    } 

    public QUser(Class<? extends User> type, PathMetadata<?> metadata, PathInits inits) { 
     super(type, metadata, inits); 
     this.role = inits.isInitialized("role") ? new QRole(forProperty("role"), inits.get("role")) : null; 
    } 

} 

下面是UserController.java到QUSER的唯一參考。 repository是UserRepository存儲庫的一個實例,它擴展了QueryDslPredicateExecutor。

package org.krams.controller; 

import java.util.List; 

import org.krams.domain.QUser; 
import org.krams.domain.Role; 
import org.krams.domain.User; 
import org.krams.repository.UserRepository; 
import org.krams.response.JqgridResponse; 
import org.krams.response.StatusResponse; 
import org.krams.response.UserDto; 
import org.krams.service.UserService; 
import org.krams.util.JqgridFilter; 
import org.krams.util.JqgridObjectMapper; 
import org.krams.util.UserMapper; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.data.domain.Page; 
import org.springframework.data.domain.PageRequest; 
import org.springframework.data.domain.Pageable; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestBody; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RequestParam; 
import org.springframework.web.bind.annotation.ResponseBody; 

... 

if (qUsername != null) 
      users = repository.findAll(QUser.user.username.like(qUsername), pageRequest); 

感謝您的幫助和評論。如果需要,我可以提供更多信息。

+0

我們可以看到'UserController'(進口和周圍線92)? –

+0

@RC。編輯我的帖子,現在你可以看到UserController.java的相關部分 –

+0

沒有'UserController'的導入和包,很難告訴 –

回答

1

我從零開始構建了一切,最後Querydsl正在工作。 我認爲這個問題是由彈簧數據jpa和querydsl的不兼容版本引起的。 現在我正在使用彈簧數據jpa 1.3.2.RELEASE和querydsl 2.8.0,一切正常。 P.S.我刪除了additionalSourceRoots和build-helper-maven-plugin配置。他們是不必要的。

+0

將spring-core,spring-data-jpa,querydsl更新到最新版本。一切正常! 彈簧核心:4.0.3.RELEASE 彈簧數據的JPA:1.5.1.RELEASE querydsl:3.3.2 我以前的彈簧數據的JPA的版本必須是太低了。 –

0

不知道,但在我目前的項目(由別人設立)我有兩個額外的文件夾,並且它們在產生來源相捆綁進去。

希望這有助於。

<!-- MYSEMA generates a lot of query classes for the model in a pre-process step --> 
    <plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>build-helper-maven-plugin</artifactId> 
    <version>${version.org.codehaus.mojo.build-helper-maven-plugin}</version> 
    <executions> 
     <execution> 
     <id>add-source</id> 
     <phase>generate-sources</phase> 
     <goals> 
      <goal>add-source</goal> 
     </goals> 
     <configuration> 
      <sources> 
      <source>${project.build.directory}/generated-sources/apt/</source> 
      <source>${project.build.directory}/generated-sources/annotations/</source> 
      </sources> 
     </configuration> 
     </execution> 
    </executions> 
    </plugin> 
    <plugin> 
    <groupId>com.mysema.maven</groupId> 
    <artifactId>apt-maven-plugin</artifactId> 
    <version>${version.com.mysema.maven.apt-maven-plugin}</version> 
    <executions> 
     <execution> 
     <goals> 
      <goal>process</goal> 
     </goals> 
     <configuration> 
      <outputDirectory>target/generated-sources/apt</outputDirectory> 
      <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor> 
     </configuration> 
     </execution> 
    </executions> 
    </plugin> 
+0

有趣 - $ {version.com.mysema.maven.apt-maven-plugin}會在你的配置中解決什麼問題? – David

0

這工作對我來說是這樣

 <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>build-helper-maven-plugin</artifactId> 
      <version>1.10</version> 
      <executions> 
       <execution> 
        <id>add-resource</id> 
        <phase>generate-sources</phase> 
        <goals> 
         <goal>add-source</goal> 
        </goals> 
        <configuration> 
         <sources> 
          <source>src/main/sources</source> 
         </sources> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 

     <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>src/main/sources</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.4.3</version> 
       </dependency> 
      </dependencies> 
     </plugin>