0

A Java web project that uses Hibernate just fell into my lap. The original architect has left and I have almost no experience with JPA's Criteria API. So please forgive my ignorance. I'm looking for help with writing several joins using JPA's Criteria API.

I have the following entities and relationships. An ElectronicDevice contains a set of Components which contains a set of Signals.
I'm trying to write a query that returns all of the components and it's signals of a specific electronic device.

I can get the ElectronicDevice with the Component, but I'm unable to get the Signals. I have the following query written.

EntityManager em = getEm();

String electronicDeviceId="Some UUID";

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(Component_.class);
Root<ElectronicDevice> i = cq.from(ElectronicDevice.class);
SetJoin<ElectronicDevice, Component> join = i.join(ElectronicDevice_.components);
cq.where(cb.equal(i.get(ElectronicDevice_.id), electronicDeviceId));
cq.multiselect(join);
TypedQuery q = em.createQuery(cq);
List<Component> resultList = q.getResultList();

My Entity Definitions are as follows

@Entity
public class ElectronicDevice implements Serializable {

    @Id
    @Column(length = 36)
    private String id;

    @XmlElement
    @OneToMany
    private Set<Component> components = new HashSet<Component>();
}

@Entity
public class Component extends ParametricObject implements Serializable{

    @XmlElement
    @OneToMany
    private Set<Signal> signals = new HashSet<Signal>();

    public Set<Signal> getComponents() {
        return signals;
    }
}

@Entity
public class Signal implements Serializable {

    @Id
    @Column(length = 36)
    private String id;
}

public class ParametricObject {

    @EmbeddedId
    @XmlElement
    private ParametricId id;

    @XmlElement
    private String name;

    public ParametricId getId() {
        return id;
    }

    public void setId(ParametricId id) {
        this.id = id;
    }

}

public class ParametricId {

    @XmlElement
    @ManyToOne
    @XmlIDREF
    private ElectronicDevice ed;

    @XmlAttribute
    @XmlID
    @Column(length = 36)
    private String internalId;

    public ElectronicDevice getEd() {
        return ed;
    }

    public void setEd(ElectronicDevice ed) {
        this.ed = ed;
    }

    public String getInternalId() {
        return internalId;
    }

    public void setInternalId(String internalId) {
        this.internalId = internalId;
    }
}
Thomas Lann
  • 1,124
  • 5
  • 17
  • 35

1 Answers1

-1

refer to the discussion at "Hibernate Criteria Join with 3 Tables". your query should looks likes this.

you must add ElectronicDevice with menyToOne annotation in the Component.(see the example at http://www.mkyong.com/hibernate/hibernate-one-to-many-relationship-example-annotation/.)

Criteria c = session.createCriteria(Component.class, "cmp");
c.createAlias("cmp.signals", "signal"); // this will do inner join
c.add(Restrictions.eq("cmp.electronicDevice", "xxxx"));
return c.list();
Community
  • 1
  • 1
  • Maybe I'm not completely understanding your answer. But this doesn't seem to be using JPA's criteria API. Is this code specific to Hibernate? – Thomas Lann Apr 27 '16 at 18:06