spring - 在Gradle中进行集成测试时未找到持久类

标签 spring hibernate jpa gradle

我的项目基于Spring + JPA + Hibernate,并且以前使用Ant来管理构建过程,现在我打算使用Gradle。当运行“渐变清洁测试”时,出现如下异常:

[2013-02-07 11:01:36,703][Test worker][WARN ][QuerySplitter] no persistent classes found for query class: from com.mpos.lottery.te.workingkey.domain.WorkingKey w where w.createDateStr=:createDateStr and w.gpeId=:gpeId

[2013-02-07 11:01:36,718][Test worker][ERROR][TEPortServlet] org.hibernate.QueryParameterException: could not locate named parameter [gpeId]; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: could not locate named parameter [gpeId]


我在eclipse和Ant中都运行了该测试用例(不需要 jetty ),它通过了,所以我怀疑gradle中的某些错误配置会导致此异常。
以下是我的gradle脚本:
apply plugin: 'war'
// 'war' plugin will apply 'java' plugin automatically
apply plugin: 'java'
apply plugin: 'eclipse'
// run web application, refer to http://gradle.org/docs/current/userguide/jetty_plugin.html
apply plugin: 'jetty'

compileJava.options.encoding = _sourcecode_encoding
compileTestJava.options.encoding = _sourcecode_encoding

// Properties added by the java plugin
sourceCompatibility="${_source_compatibility}"
targetCompatibility="${_target_compatibility}"
//Properties added by the 'war' plugin
webAppDirName="src/main/WWW"

configurations {
    provided {
        description = 'Non-exported comiple-time dependencies, it will be provided by container.'
    }
}

dependencies {
    provided files('lib/DEV/j2ee/servlet-api.jar')
    compile fileTree(dir: 'lib', include: '**/*.jar', exclude: 'DEV/**/*.jar')
    //compile sourceSets.main.output
    testCompile fileTree(dir:"lib", include:"DEV/**/*.jar")
}

sourceSets {
    main {
        compileClasspath = compileClasspath + configurations.provided
        //compileClasspath.collect().each({println it})
        resources {
            srcDir 'src/main/resource'
        }
    }
    test {
        resources {
            srcDir 'src/test/resource'
        }
    }
}

test {
    scanForTestClasses  = false
    classpath = configurations.provided + configurations.compile + configurations.testCompile + sourceSets.main.output + sourceSets.test.output
    classpath.each({println it})
    // customize test process
    include 'com/mpos/lottery/te/gameimpl/smsraffle/**/*Test.class'
}
我是Gradle和groovy的新手,请帮助我。提前致谢。

最佳答案

默认情况下,Gradle为类和资源使用单独的输出目录,这可能会导致Hibernate / JPA问题。尝试:

sourceSets.main.output.resourcesDir = sourceSets.main.output.classesDir
sourceSets.test.output.resourcesDir = sourceSets.test.output.classesDir

您可能不需要后者。

关于spring - 在Gradle中进行集成测试时未找到持久类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14742941/

相关文章:

java - Hibernate实体没有被持久化

java - Hibernate Criteria - 从两个没有任何关联的表中获取结果

java - Hibernate:可嵌入列表的唯一约束(@ElementCollection)

mysql - 如何使用 JPA/Hibernate 设置复合主键的列顺序

java - 多个项目中的实体

java - 如何在 Spring Web 应用程序中实现共享功能?

java - Spring Security OAuth2 的 Web 和移动客户端

java - 如何保留分离的对象?

java - 如何正确检查数据库中是否存在行?

java - @NamedNativeQuery 和@SqlResultSetMapping 用于非实体