Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CriteriaBuilder doesn't work with ELEMENT_COLLECTION #503

Open
Oribow opened this issue Feb 8, 2022 · 0 comments
Open

CriteriaBuilder doesn't work with ELEMENT_COLLECTION #503

Oribow opened this issue Feb 8, 2022 · 0 comments

Comments

@Oribow
Copy link

Oribow commented Feb 8, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant