Polymorphic CriteriaQuery without inverse relation

2019-03-18 07:18发布

问题:

I have the following EJB structure. Don't wonder about Animal and Inventory, these classes are only here to demonstrate the structure in a simplified way (Update: I have revised the class names to construct a better understandable example. Another implementation of IdTag might be a BarcodeId). Note that there is no inverse relationship from IdTag to Animal or Inventory, and let's assume the RfidTag.code is unique. I read Retrieving Polymorphic Hibernate Objects Using a Criteria Query and Hibernate polymorphic query but these discussions does not seem to answer my question.

public interface ItemWithIdTag
{
    IdTag getIdTag();
    void setIdTag(IdTag idTag);
}

@Entity public class Animal implements ItemWithIdTag,Serializable
{
    @Id @GeneratedValue(strategy=GenerationType.AUTO) private long id;

    @OneToOne(cascade = CascadeType.ALL)
    private IdTag idTag;
}

@Entity public class Inventory implements ItemWithIdTag,Serializable
{
    @Id @GeneratedValue(strategy=GenerationType.AUTO) private long id;

    @OneToOne(cascade = CascadeType.ALL)
    private IdTag idTag;
}

@Entity @Table(name = "IdTag") @Inheritance(strategy= InheritanceType.JOINED)
public class IdTag implements Serializable
{
    @Id @GeneratedValue(strategy=GenerationType.AUTO) private long id;
    private Date created;
}

@Entity @Table(name = "RfidTag")
public class RfidTag extends IdTag implements Serializable
{
    private String code;
}

Now I want to query either Animal or Inventory for a given RfidTag.code like Animal ejb = bean.fEntityWithRfidTag(Animal.class,"myRfIdCode");

public <T extends ItemWithIdTag> T fOwner(Class<T> type, String catName)
{
    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
    CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(type);
    Root<T> from = criteriaQuery.from(type);

    Path<Object> path = from.join("idTag").get("code");

    CriteriaQuery<T> select = criteriaQuery.select(from);
    select.where(criteriaBuilder.equal(path, catName));

    TypedQuery<T> q = em.createQuery(select); 
    T result = (T)q.getSingleResult();}
    return result;
}

Unfortuately I get the following errror:

javax.ejb.EJBException: java.lang.IllegalArgumentException:
Unable to resolve attribute [code] against path [null]

I assume that this is related to the inheritance IdTag -> RfidTag and Animal only knows about IdTag and not the RfidTag.code. Are queries like this possible?

回答1:

If you are using EclipseLink, solution is simple. Modify the Path criteria to cast to RfIdTag:

Path<Object> path = ((Path) from.join("idTag").as(RfIdTag.class)).get("code");

If you are using Hibernate, replace your method with:

public static <T extends ItemWithIdTag> T fOwner(Class<T> type, String catName) {
    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
    CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(type);
    Root<T> fromType = criteriaQuery.from(type);
    Root<RfIdTag> fromRfId = criteriaQuery.from(RfIdTag.class);

    Path<Object> pathCode = fromRfId.get("code");
    Path<Object> pathIdTagType = fromType.get("idTag");
    Path<Object> pathIdTagRfId = fromRfId.get("id");

    CriteriaQuery<T> select = criteriaQuery.select(fromType);
    select.where(
            criteriaBuilder.equal(pathCode, catName),
            criteriaBuilder.equal(pathIdTagType, pathIdTagRfId));

    TypedQuery<T> q = em.createQuery(select);
    return q.getSingleResult();
}

This makes a "join" ("a filtered cartesian product") between "T" and "RfIdTag".