max_fetch_depth only affect how and when proxy will be initialized, but it won't stop it from initializing.
The object that hibernate return on your query is a complex object when almost always you will see a proxy instead of an association.
This parameter will detemine how many proxies will be initialized right away. But if you call not-initialized proxy it will fetch the data for you.
When
magpor is needed a DTO.
b.t.w. no need to use reflection when building DTO and no need to catch lazy exceptions because DTO must be build with open session.
It will look something like this:
Code:
Session session = hibernateService.openSession(); // get the session
MyOject object = session.get(MyOject.class, ID); // load Hibernate Object
MyDTO dto = new MyDTO(object); // create DTO from loaded object
session.close(); // close session
return dto; // return DTO, that has no relation with session at all
and DTO could look like
Code:
public class MyDTO {
protected String name;
protected Set children;
public MyDTO(MyObject object) {
this.name = object.getName();
....
this.children = new HashSet(object.getChildren().size());
for (Object child : object.getChildren()) {
ChildDTO _child = new ChieldDTO(child);
this.children.add(_child);
}
// or if children are simple type
this.children = new HashSet(object.getChildren().size());
this.children.addAll(object.getChildren());
}
}