I'm curious about how Hibernate handles the FetchType.
I have the following domain model:
Code:
@Entity
public class T {
...
@OneToMany(fetch = FetchType.EAGER, mappedBy = "t", cascade = { CascadeType.REMOVE, CascadeType.REFRESH })
private List<TDL> tdls;
...
}
@Entity
public class TDL {
...
@ManyToOne(fetch = FetchType.LAZY)
private T t;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private ITDL itdl;
...
}
@Entity
public class ITDL {
...
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "itdl")
private DSL dsl;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy="itdl")
private TETD ittdl;
...
}
@Entity
public class DSL {
...
}
@Entity
public class TETD {
...
}
When I write a JPQL query such as:
Code:
select t from T t where t.id = 1
The result set contains "t" with eagerly loaded "tdls". Each "tdl" contains an eagerly loaded "itdl".
Now, because I specified that the ITDL object should lazily load "dsl" and "ittdl" I would expect that each "itdl" would not have "dsl" and "ittdl" loaded. However, I am finding that "dsl" and "ittdl" were actually loaded from this simple jpql query.
I know that @OneToOne and @ManyToOne default to Eager loading while @OneToMany and @ManyToMany default to Lazy loading. In this example I overrode the default @OneToMany on the T object, and the @OneToOne on the ITDL object.
Are the fetch modes in the JPQL query derived by the annotations specified on the entities or do they use the default modes? Can anyone explain why the "dsl" and "ittdl" are NOT lazily loaded?
And, my biggest question is how can I lazily load, from within jpql, @OneToOne relationships. Normally I wouldn't want to do this but because of the complex relationships defined in this entity model and the dataset, the eager fetching of the DSL and ITTDL objects are too expensive. In this case, I don't need these objects loaded and would prefer to lazily load them.