Two entities with a simple Entity graph:
@Entity
@NamedEntityGraph(name = "carWithProducer", attributeNodes = {
@NamedAttributeNode("producer")
})
class Car {
@Id @Column(name = "id")
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
private Producer producer;
}
@Entity
class Producer {
@Id @Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@OneToMany(mappedBy = "producer", fetch = FetchType.LAZY)
private Set<Car> cars;
}
First query that loads Producer together with Car:
final EntityGraph eg = em.createEntityGraph("carWithProducer");
em.createQuery("SELECT c FROM Car c")
.setHint("javax.persistence.fetchgraph", eg)
.getResultList();
But this second query does not:
final EntityGraph eg = em.createEntityGraph("carWithProducer");
em.createQuery("SELECT c FROM Car c WHERE c.producer.name = :p")
.setHint("javax.persistence.fetchgraph", eg)
.setParameter("p", producerName)
.getResultList();
First query generates following SQL:
select
car0_.id as id1_0_0_,
producer1_.id as id1_1_1_,
car0_.type_id as type_id2_0_0_,
producer1_.name as name2_1_1_
from
CAR car0_
left outer join
PRODUCER producer1_
on car0_.type_id=producer1_.id
Second query generates following SQL:
select
car0_.id as id1_0_,
car0_.type_id as type_id2_0_
from
CAR car0_ cross
join
PRODUCER producer1_
where
car0_.type_id=producer1_.id
and producer1_.name=?
|