Skip to content

where {} queries fail when accessing properties of embedded components #15955

Description

@gsartori

where {} queries fail when accessing a property of an embedded component, even though the property is referenced using the expected dotted notation.

Hibernate throws the following exception:

org.hibernate.QueryException: Criteria objects cannot be created directly on components.
Create a criteria on owning entity and use a dotted property to access component property: extRef1

The query already uses a dotted property (extRef1.value), so it appears that GORM is translating the where {} closure into a Hibernate Criteria incorrectly.

The same happens with:

extRef1.provider == "SAP"

so the issue does not appear to be related to the =~ operator, but to accessing any property of an embedded component.

Expected behavior

The query should be translated into a predicate on the embedded property (e.g. extRef1.value) and execute successfully.

Actual Behaviour

Hibernate throws:

org.hibernate.QueryException: Criteria objects cannot be created directly on components.
Create a criteria on owning entity and use a dotted property to access component property: extRef1

NOTE: Removing the extRef1.value predicate makes the query execute correctly.

Steps To Reproduce

Domain model

class WorkItem extends SystemManaged {
    String description
}

Classes in /main/src

abstract class SystemManaged implements GormEntity {
    ExternalRef extRef1

    static embedded = ['extRef1']
}

class ExternalRef {
    String provider
    String value
}

Query

def query = WorkItem.where {}

String search = "ABC"

query = query.where {
    extRef1.value =~ "%${search}%"
}

Environment Information

Grails: 7.1.0

Example Application

No response

Version

7.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions