1

I have a method throwing the next error when querying a list of Hibernate entities while also fetching with a Join a OneToOne relation, having the child an @EmbeddedId .

org.springframework.orm.hibernate3.HibernateSystemException: Unable to resolve property: id.element; nested exception is org.hibernate.HibernateException: Unable to resolve property: id.element

This is the query method:

public List<Element> findAll() {
  CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();

  CriteriaQuery<Element> criteriaQuery = criteriaBuilder.createQuery(Element.class);
  Root<Element> root = criteriaQuery.from(Element.class);

  criteriaQuery.select(root).distinct(true);

  root.fetch(Element_.elementSubstitute, JoinType.LEFT);

  return getEntityManager().createQuery(criteriaQuery).getResultList();;
}

This works fine when there are no resulting elementSubstitutes. Also similar methods work well for other non-embedded-entities, but these ones are mapped using an @EmbeddedId like this, which is why I think is the cause of the error.

@Entity
@Table(name = "ELEMENT")
public class Element {

        //stuff

        @OneToOne(mappedBy = "id.element", fetch = FetchType.LAZY)
        private ElementSubstitute elementSubstitute;
}

@StaticMetamodel(Element.class)
public class Element_ {

    //stuff

    public static volatile SingularAttribute<Element, ElementSubstitute> elementSubstitute;
}

@Entity
@Table(name = "ELEMENT_SUBSTITUTE")
public class ElementSubstitute {

    @EmbeddedId
    private ElementSubstituteId id = new ElementSubstituteId();

    //stuff
}

@Embeddable
public class ElementSubstituteId {

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ID_OWNER")
    private Owner owner;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ID_ELEMENT")
    private Element element;
}

Obtaining the ElementSubstitute from a single Element entity with getElementSubstitute() also works, so the mapping is not wrong and Hibernate is indeed able to recognize the property id.element when he wants to. Why is it failing when fetching with a join on a query?

p4x
  • 384
  • 1
  • 5
  • 16

0 Answers0