apache / apache/grails-core

CriteriaBuilder doesn't work with ELEMENT_COLLECTION

Open
#14,374 0 comments 0 reactions 0 assignees View on GitHub
relates-to: grails-data-hibernate5
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

I have a domain object with a collection of non-domain objects.
```
class Person{
static hasMany = [knownContacts: KnownContacts]
}

enum KnownContacts{
None,
Email,
Phone
}
```
I want to use this "association" in a criteria, but that throws a `groovy.lang.MissingMethodException: No signature of method` error at `knownContacts { ... }`.

```
{
projections {
knownContacts {
id()
}
}
}
```

This is because gorm treats the "knownContacts" not as an association, but as an ELEMENT_COLLECTION.
Which means the isAssociation check fails on it, as its PersistentAttributeType is ELEMENT_COLLECTION.
```
public boolean isAssociation() {
return getPersistentAttributeType() == PersistentAttributeType.ONE_TO_MANY
|| getPersistentAttributeType() == PersistentAttributeType.MANY_TO_MANY;
}
```
There doesn't seem to be any code in AbstractHibernateCriteriaBuilder.invokeMethod that deals with ELEMENT_COLLECTIONs.

```
if (pd != null && pd.getReadMethod() != null) {
final Metamodel metamodel = sessionFactory.getMetamodel();
final EntityType entityType = metamodel.entity(targetClass);
final Attribute attribute = entityType.getAttribute(name);

if (attribute.isAssociation())
[...]
```
The `attribute.isAssociation` check fails and the `throw new MissingMethodException(name, getClass(), args);` at the end of the method is reached.

I'm not sure if this is intentional or an oversight. Is there a different way we are supposed to query non domain to domain associations?

The generated database schema creates an extra table for ELEMENT_COLLECTION (at least in this case) so I'm not sure why they can't be used like regular associations between domain objects.

Contributor guide

Open the contributing guide

Research direction

Reproduce the Person/knownContacts criteria projection with the enum-backed ELEMENT_COLLECTION example. Start in AbstractHibernateCriteriaBuilder.invokeMethod and inspect how the metamodel attribute and isAssociation check handle ELEMENT_COLLECTION. Done means establishing the intended query behavior and adding coverage for the knownContacts projection or documenting the supported alternative.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.