I have a complex JPA/Hibernate problem. I have two entities A and B. A references B, but this relationship is not required.
public class A {
@JoinColumn(name = "b_id", referencedColumnName = "b_id")
@OneToOne
private B b;
}
I write SELECT NEW dto(a.b) FROM A a this generates inner join, which isn't good for me, because a.b can be null and I want all of the A entites. So I rewrite my query like this: SELECT NEW dto(b) FROM A a LEFT JOIN B b We have to use alias (b) to prevent inner join and get only the outer join. Ok I get all line, but I get extra queries, Hibernate generates SELECT queries for all of B entities. Why, how can I stop this, I use left join, because I want only one query.
Edit:
The same problem with inner joins. If use SELECT NEW dto(a.b) FROM A a additional SELECT b FROM B b WHERE b.id = ? occures, but if I call SELECT NEW dto(a.b.id) FROM A a there isn't additional select for B entites.
Edit2:
This is exactly same as my problem: Same problem, without possible solution.
Because I don't load the parent object itself, Hibernate doesn't load eagerly B...
you want the Bs referenced from A and since you are using hql:
SELECT NEW dto(b) FROM A a LEFT JOIN a.b b
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With