Jin Kwon
Jin Kwon

Reputation: 22017

Join from left side without references for @ManyToMany

I have three tables.

LEFT ---------|<- LEFT_RIGHT ->|--------- RIGHT
@Entity
class Left {
    // no collection mapping for LeftRight
}

@Entity
class Right {
    // no collection mapping for LeftRight
}

@Entity
class LeftRight {

    @JoinColumn(name="LEFT_ID")
    @ManyToOne
    private Left left;

    @JoinColumn(name="RIGHT_ID")
    @ManyToOne
    private Right right;
}

Note that neither Left nor Right doesn't have any reference for LeftRights.

Is there any way with Criteria-API to selecting from Left while joining to LeftRight?

Upvotes: 0

Views: 123

Answers (2)

James
James

Reputation: 18379

Yes, just use a selection.

CriteriaQuery cq = cb.createQuery();
Root r = cq.from(LeftRight.class);
cq.select(r.get("left"));
cq.where(...);
Query query = em.createQuery(cq);

http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/Criteria#CriteriaQuery

Upvotes: 0

MikkolidisMedius
MikkolidisMedius

Reputation: 4844

You could create a criteria query for LeftRight and use projection to retrieve the left property:

session.createCriteria(LeftRight.class)
    // Add whatever restrictions on Left properties
    .createCriteria("left")
    .add(Restrictions.like("name", "%something%"))
    // Use projection to retrieve the left property
    .setProjection(Projections.property("left"));

Upvotes: 1

Related Questions