Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA2 Criteria: How to avoid a cross join using path.get()

If you have this entity:

@Entity
public class A {

    @ManyToOne
    @JoinColumn(name = "bField", nullable = true)
    private B myBObject;

}

And I have a generic generator of Criteria who will do that:

Root<A> root = criteria.from(A.class);
root.get("myBObject").get("aFieldInB");

The problem is the following: the generated sql will contains a CROSS JOIN between A and B. But I would like that the generated sql will contains a LEFT JOIN between A and B.

How can I do that?

like image 755
user1180339 Avatar asked Apr 10 '13 10:04

user1180339


1 Answers

You must use a join(). In general it is better to always use a join() for relationships.

See, http://en.wikibooks.org/wiki/Java_Persistence/Criteria#JoinType

like image 88
James Avatar answered Oct 31 '22 11:10

James