Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jpa 使用gradle为groovy生成Querydsl代码_Jpa_Groovy_Gradle_Querydsl - Fatal编程技术网

Jpa 使用gradle为groovy生成Querydsl代码

Jpa 使用gradle为groovy生成Querydsl代码,jpa,groovy,gradle,querydsl,Jpa,Groovy,Gradle,Querydsl,我在groovy中定义了实体bean。我无法为groovy中的实体生成querydsl代码 这对于Java中的实体bean很好,但对于groovy则不行 参考上的Querydsl文档,它指出必须使用GenericExporter为groovy实体生成代码。但是我不知道如何在gradle中配置GenericExporter 如果有人能够使用gradle为groovy中的实体生成querydsl代码,请与我们分享 谢谢我今天刚刚想出了一个可行的解决方案。它结合了由提供的信息和我在github上找到的

我在groovy中定义了实体bean。我无法为groovy中的实体生成querydsl代码

这对于Java中的实体bean很好,但对于groovy则不行

参考上的Querydsl文档,它指出必须使用
GenericExporter
为groovy实体生成代码。但是我不知道如何在gradle中配置
GenericExporter

如果有人能够使用gradle为groovy中的实体生成querydsl代码,请与我们分享


谢谢

我今天刚刚想出了一个可行的解决方案。它结合了由提供的信息和我在github上找到的信息(非常感谢作者)

我在github上找到的构建脚本已针对Scala进行了调整,但经过一些修改,我使它能够与groovy一起工作。我给你我的整个体形,下面是格拉德尔。这是一个全新的春季启动项目

注意,我可能会删除一些与问题无关的东西(比如jadira/joda库)。我留下它们是为了说明您必须将它们作为项目的编译依赖项和构建脚本依赖项包含

发生这种情况是因为Querydsl的GenericeExporter需要与它扫描的编译类相关的所有内容,否则它将失败(对我来说就是这样)

最后,我有意保留GenericeExporter配置中使用的完全限定类名,这样我就不会用“import”语句污染整个构建脚本

我的解决方案可能不是最好的,所以我愿意接受评论。目前,其工作原理如下:

  • 您可以运行
    /gradle compileGroovy
    来构建手动编写的类(包括您在Groovy中编写的
    @Entity
    类)`
  • 您运行
    /gradle generateqlsl
    ,它应该将“Q”类生成为
    src/main/generated
  • 最后再次运行
    /gradle compileGroovy
    重新构建所有内容,这次包括先前由
    generateqdsl
    生成的源代码
  • 我用gradle 1.11和2.1测试了它。我希望它也适用于你

    
    
        //
        // Configure the build script itself
        //
        buildscript {
            ext {
                querydslGeneratedSourcesDir = file("src/main/generated")
                querydslInputEntityPackage = "my.project.domain"
                querydslVersion = "3.6.0"
                groovyVersion = "2.3.8"
    
                jadiraVersion = "3.2.0.GA"
                jodaTimeVersion = "2.6"
                jodaMoneyVersion = "0.10.0"
    
                hibernateJpaApiVersion = "1.0.0.Final"
            }
    
            repositories {
                mavenLocal()
                mavenCentral()
            }
    
            dependencies {
                // Load Spring Boot plugin
                classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.9.RELEASE")
    
                // Required by QueryDSL GenericExporter
                classpath("com.mysema.querydsl:querydsl-codegen:${querydslVersion}")
                classpath("org.hibernate.javax.persistence:hibernate-jpa-2.1-api:${hibernateJpaApiVersion}")
    
                classpath files("${buildDir}/classes/main")
                classpath("org.jadira.cdt:cdt:${jadiraVersion}")
                classpath("joda-time:joda-time:${jodaTimeVersion}")
                classpath("org.joda:joda-money:${jodaMoneyVersion}")
            }
        }
    
        apply plugin: 'java'
        apply plugin: 'groovy'
        apply plugin: 'eclipse'
        apply plugin: 'idea'
        apply plugin: 'spring-boot'
    
        //
        // Make both java and groovy files visible only to the Groovy Compiler plugin
        //
        sourceSets {
            main {
                java {
                    srcDirs = []
                }
                groovy {
                    srcDirs = ['src/main/java', querydslGeneratedSourcesDir]
                }
            }
            test {
                java {
                    srcDirs = []
                }
                groovy {
                    srcDirs = ['src/test/java']
                }
            }
        }
    
        task generateQueryDSL(group: 'build', description: 'Generate QueryDSL query types from compiled entity types') {
            new com.mysema.query.codegen.GenericExporter().with {
                setKeywords(com.mysema.query.codegen.Keywords.JPA)
                setEntityAnnotation(javax.persistence.Entity.class)
                setEmbeddableAnnotation(javax.persistence.Embeddable.class)
                setEmbeddedAnnotation(javax.persistence.Embedded.class)
                setSupertypeAnnotation(javax.persistence.MappedSuperclass.class)
                setSkipAnnotation(javax.persistence.Transient.class)
                setTargetFolder(querydslGeneratedSourcesDir)
                export(querydslInputEntityPackage)
            }
        }
    
        //
        // Configure spring boot plugin
        //
        bootRepackage {
            mainClass = 'my.project.Application'
        }
    
        jar {
            baseName = 'gs-spring-boot'
            version = '0.1.0'
        }
    
        repositories {
            mavenLocal()
            mavenCentral()
        }
    
        dependencies {
            compile("org.springframework.boot:spring-boot-starter-web")
            // tag::actuator[]
            compile("org.springframework.boot:spring-boot-starter-actuator")
            // end::actuator[]
            compile("org.springframework.boot:spring-boot-starter-data-jpa")
        //    compile("org.springframework.boot:spring-boot-starter-security")
    
            compile("mysql:mysql-connector-java:5.1.34")
            compile("org.codehaus.groovy:groovy-all:${groovyVersion}")
            compile("cz.jirutka.spring:spring-rest-exception-handler:1.0.1")
    
            compile("com.mysema.querydsl:querydsl-core:${querydslVersion}")
    
            // Joda & Jadira types
            compile("joda-time:joda-time:${jodaTimeVersion}")
            compile("org.joda:joda-money:${jodaMoneyVersion}")
            compile("org.jadira.usertype:usertype.extended:${jadiraVersion}")
            compile("org.jadira.cdt:cdt:${jadiraVersion}")
            compile("com.fasterxml.jackson.datatype:jackson-datatype-joda:2.4.4")
    
            // Testing
            testCompile("junit:junit")
            testCompile("org.springframework.boot:spring-boot-starter-test")
        }
    
        // Configure the gradle wrapper
        task wrapper(type: Wrapper) {
            gradleVersion = '2.1'
        }
    
    谢谢分享

    找不到向groovy生成queryDSL的其他解决方案。 我做了一些修改,现在可以做到:

    ./gradlew clean build
    
    从.java和.groovy文件生成“Q文件”,当然还要编译它们

    我将示例推送到github:

    但我不知道为什么,只有当我运行两次“/gradlew clean build”时,这才起作用

    谢谢