Skip to content

Interpolated class name in string based HQL query throws exception in 6.1.x #916

@dpcasady

Description

@dpcasady

Running a string based query (findAll, executeQuery, executeUpdate, etc.) with an interpolated class name no longer works in 6.1.x:

void "test findAll with interpolated class name"() {
    given:
        String className = "Book"
    expect:
        Book.findAll("from $className") == [] // throws GrailsQueryException
}

Here's the full stacktrace for the failing integration test above:

org.grails.orm.hibernate.exceptions.GrailsQueryException: Invalid query [from :p0] for domain class [hql.gstring.Book]
	at org.grails.orm.hibernate.AbstractHibernateGormStaticApi.findAll(AbstractHibernateGormStaticApi.groovy:294)
	at org.grails.orm.hibernate.AbstractHibernateGormStaticApi.findAll(AbstractHibernateGormStaticApi.groovy:382)
	at org.grails.datastore.gorm.GormEntity$Trait$Helper.findAll(GormEntity.groovy:1247)
	at hql.gstring.BookHqlSpec.$tt__$spock_feature_0_1(BookHqlSpec.groovy:20)
	at hql.gstring.BookHqlSpec.test findAll with interpolated class name_closure2(BookHqlSpec.groovy)
	at groovy.lang.Closure.call(Closure.java:414)
	at groovy.lang.Closure.call(Closure.java:430)
	at grails.transaction.GrailsTransactionTemplate$1.doInTransaction(GrailsTransactionTemplate.groovy:70)
	at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
	at grails.transaction.GrailsTransactionTemplate.executeAndRollback(GrailsTransactionTemplate.groovy:67)
	at hql.gstring.BookHqlSpec.test findAll with interpolated class name(BookHqlSpec.groovy)

This used to work prior to 6.1.x, and I believe this issue was introduced while working on #827 . Looks like the current code assumes that any interpolated value is a query parameter.

Example grails 3.2.8/GORM 6.1.1 app that demonstrates the issue. Run the integration tests and you should see the exception.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions