hibernate - Grails单元测试失败:此实现GORM当前不支持基于字符串的查询,例如[executeQuery]

标签 hibernate grails hql gorm spock

SO上还有其他问题,但是它们已经很老了,没有一个可以解决我的问题。

我从单元测试中收到以下错误:

java.lang.UnsupportedOperationException: String-based queries like [executeQuery] are currently not supported in this implementation of GORM. Use criteria instead.
at org.grails.datastore.gorm.GormStaticApi.unsupported(GormStaticApi.groovy:984)
at org.grails.datastore.gorm.GormStaticApi.executeQuery(GormStaticApi.groovy:896)
at org.grails.datastore.gorm.GormStaticApi.executeQuery(GormStaticApi.groovy:892)
at org.grails.datastore.gorm.GormEntity$Trait$Helper.executeQuery(GormEntity.groovy:1026)
at com.mycompany.SomeService.$tt__getSomeThing(SomeService.groovy:54)
at com.mycompany.SomeService.getSomeThing_closure1(SomeService.groovy)
at groovy.lang.Closure.call(Closure.java:414)
at groovy.lang.Closure.call(Closure.java:430)
at grails.transaction.GrailsTransactionTemplate$2.doInTransaction(GrailsTransactionTemplate.groovy:96)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at grails.transaction.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:93)
at com.mycompany.SomeServiceSpec.nominal getSomeThing(SomeServiceSpec.groovy:45)

打破测试的行是:
def something = SomeDomain.executeQuery("some HQL string")

当前测试如下(仅跟踪当前数据):
    @TestFor(SomeService)
    @Mock(SomeDomain)
    class SomeServiceSpec extends Specification {

        def setup() {
        }

        def cleanup() {
        }

        void "nominal getSomeThing"() {
            setup:
            def args = ['thingId':1l]

            when:
            def something = service.getSomething(args)

            then:
            println "Thing: ${something}"
        }
    }

我的设置:
Grails 3.1.12
Hibernate 4 (I upgraded to 5 with no change in behavior)
Java 1.8
Groovy 2.4.7

最佳答案

尝试使用HibernateTestMixin。它在Hibernate 4中支持基于字符串的HQL查询。

import grails.test.mixin.TestMixin
import grails.test.mixin.gorm.Domain
import grails.test.mixin.hibernate.HibernateTestMixin

@TestFor(SomeService)
@Domain(SomeDomain)
@TestMixin(HibernateTestMixin)
class SomeServiceSpec extends Specification {

    def setup() {
    }

    def cleanup() {
    }

    void "nominal getSomeThing"() {
        setup:
        def args = ['thingId':1l]

        when:
        def something = service.getSomething(args)

        then:
        println "THING: ${something}"
    }
}

您还需要添加到build.gradle:
dependencies {
    testCompile 'org.grails:grails-datastore-test-support'
}

关于hibernate - Grails单元测试失败:此实现GORM当前不支持基于字符串的查询,例如[executeQuery],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50516956/

相关文章:

grails - Grails 3.3.1控制台提供404

grails - 对地址栏中直接插入的链接进行Geb测试

grails - 租户服务不可用 Grails 3.3.2 gorm 6.1.8.RELEASE

java - 通过HQL从两个表中获取数据并插入到一个表中

java - RequestMapping 与 hibernate OneToMany 如何以正确的方式插入外键

java.lang.ClassCastException : java. util.ArrayList 无法转换为 ....CrExcessMaster

java - 实体/行仅有时使用 Spring/Hibernate 更新?

java - Spring Bean/存储库的 ID

java - 按 varchar 字段中存储的整数值对表进行排序

hibernate - gorm/grails排序具有许多子项