"IllegalArgumentException: Unable to locate Attribute with the the given name [someProperty] on this ManagedType [unknown]" in Hibernate

Solution Verified - Updated -

Issue

  • A class hierarchy is defined with a @MappedSuperclass and two or more concrete @Entity annotated types.

    @MappedSuperclass
    public abstract class AbstractEntity<Key extends AbstractKey> implements Serializable {
        @EmbeddedId
        protected Key key;
        ...
    }
    
    @Embeddable
    public class ConcreteKeyOne extends AbstractKey {
        @Column(nullable=false)
        public String one;
        ...
    }
    
    @Embeddable
    public class ConcreteKeyTwo extends AbstractKey {
        @Column(nullable=false)
        public String two;
        ...
    }
    
    @Entity
    public class ConcreteEntityOne extends AbstractEntity<ConcreteKeyOne> {
        ...
    }
    
    
    @Entity
    public class ConcreteEntityTwo extends AbstractEntity<ConcreteKeyTwo> {
        ...
    }
    
  • A criteria query is constructed and fails with an IllegalArgumentException when trying to locate the key property

        CriteriaQuery cq = cb.createQuery(ConcreteEntityOne.class);
        Root<ConcreteEntityOne> root = cq.from(ConcreteEntityOne.class);
        Path<ConcreteKeyOne> id = root.get("key");
        Path<String> name = id.get("one"); // fails with java.lang.IllegalArgumentException
        ...
    
  • The attempt to create a path to the key property (one) fails with:

    ... java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [one] on this ManagedType [unknown]
        at org.hibernate.metamodel.internal.AbstractManagedType.checkNotNull(AbstractManagedType.java:128)
        at org.hibernate.metamodel.internal.AbstractManagedType.getAttribute(AbstractManagedType.java:113)
        at org.hibernate.query.criteria.internal.path.SingularAttributePath.locateAttributeInternal(SingularAttributePath.java:71)
        at org.hibernate.query.criteria.internal.path.AbstractPathImpl.locateAttribute(AbstractPathImpl.java:204)
        at org.hibernate.query.criteria.internal.path.AbstractPathImpl.get(AbstractPathImpl.java:177)
        ...
    
  • It appears that the key/ID properties for the last entity processed when the internal metamodel is constructed have overwritten all previous properties (for different key/entity pairs).
  • The same code appears to have worked in Hibernate 4 / EAP 6.

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP) 7
  • Hibernate 5

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content