Hi all
I have two classes, Test2 and Test3. Test2 has an attribute test3 that is an instance of Test3. In other words, I have a unidirectional OneToOne association, with test2 having a reference to test3.
When I select Test2 from the db, I can see that a separate select is being made to get the details of the associated test3 class. This is the famous 1+N selects problem.
To fix this to use a single select, I am trying to use the fetch=join annotation, which I understand to be @Fetch(FetchMode.JOIN)
However, with fetch set to join, I still see separate selects. Here are the relevant portions of my setup..
hibernate.cfg.xml:
<property name="max_fetch_depth">2</property>
Test2:
public class Test2 {
@OneToOne (cascade=CascadeType.ALL , fetch=FetchType.EAGER)
@JoinColumn (name="test3_id")
@Fetch(FetchMode.JOIN)
public Test3 getTest3() {
return test3;
}
NB I set the FetchType to EAGER out of desperation, even though it defaults to EAGER anyway for OneToOne mappings, but it made no difference.
Thanks for any help!
Edit: I've pretty much given up on trying to use FetchMode.JOIN - can anyone confirm that they have got it to work ie produce a left outer join?
In the docs I see that "Usually, the mapping document is not used to customize fetching. Instead, we keep the default behavior, and override it for a particular transaction, using left join fetch in HQL"
If I do a left join fetch instead:
query = session.createQuery("from Test2 t2 left join fetch t2.test3");
then I do indeed get the results I want - ie a left outer join in the query.