QueryDSL为SQLDialect生成JPQL而不是SQL

时间:2016-04-28 06:11:52

标签: spring-boot querydsl

尝试在下面获取本地SQL的QueryDSL是Spring启动POM代码段

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

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.7</java.version>
        <querydsl.version>4.0.1</querydsl.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-sql</artifactId>
            <version>${querydsl.version}</version>
        </dependency>
        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-sql-codegen</artifactId>
            <version>${querydsl.version}</version>
            <scope>provided</scope>
        </dependency> 
    </dependencies>

    <build>
        <plugins>

             .....

            <plugin>
                <groupId>com.querydsl</groupId>
                <artifactId>querydsl-maven-plugin</artifactId>
                <version>${querydsl.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>export</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <jdbcDriver>org.hsqldb.jdbc.JDBCDriver</jdbcDriver>
                    <jdbcUrl>jdbc:hsqldb:file:${project.basedir}/db/init;files_readonly=true</jdbcUrl>
                    <jdbcUser>sa</jdbcUser>
                    <schemaPattern>PUBLIC</schemaPattern>
                    <packageName>com.example.domain</packageName>
                    <targetFolder>${project.basedir}/src/generated/java</targetFolder>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.hsqldb</groupId>
                        <artifactId>hsqldb</artifactId>
                        <version>2.2.4</version>
                    </dependency>
                    <dependency>
                        <groupId>ch.qos.logback</groupId>
                        <artifactId>logback-classic</artifactId>
                        <version>1.1.5</version>
                    </dependency>
                </dependencies>
            </plugin>

        </plugins>
    </build>

执行查询的代码

void test() throws Exception {
    QCustomer customer = QCustomer.customer;
    System.out.println(dataSource.getConnection().getMetaData().getDatabaseProductName());
    SQLTemplates dialect = SQLTemplates.DEFAULT;
    SQLQuery query = new SQLQuery(dataSource.getConnection(), dialect);
    System.out.println(query.from(customer.count()).fetch());
}

执行结果为SQLException,因为库生成JPQL(或HQL)而不是Native SQL

Exception in thread "main" com.querydsl.core.QueryException: Caught SQLSyntaxErrorException for 
from count(CUSTOMER.ID)
    at com.querydsl.sql.DefaultSQLExceptionTranslator.translate(DefaultSQLExceptionTranslator.java:50)
    at com.querydsl.sql.Configuration.translate(Configuration.java:451)
    at com.querydsl.sql.AbstractSQLQuery.fetch(AbstractSQLQuery.java:399)
    at com.example.DemoApplication$Test.test(DemoApplication.java:55)
    at com.example.DemoApplication.main(DemoApplication.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.sql.SQLSyntaxErrorException: unexpected token: FROM

1 个答案:

答案 0 :(得分:0)

我们应该使用

query.select(customer.count())
    .from(customer)
    .fetch()

对于QueryDSL 4.X及更高版本(最新版本),请参阅http://www.querydsl.com/static/querydsl/latest/reference/html/ch02s03.html#d0e1145