Hi,
FetchProfiles have a great potential in reducing the performance headaches that are inherent to Hibernate's automatic query generation. For me, Fetch profiles are not just about eager fetching of lazy associations but also about preventing Hibernate from generating all sorts of unwanted (left) joins when retrieving an entity that can cause all sorts of performance bottlenecks at the database level (tmp tables, file sorting etc). This also means that FetchProfiles are relevant in activating those joins when you do want the associations to load.
I have some questions about the limitations I seem to be running into (I'm using Hibernate 3.5.4.):
Let's say we have the following classes:
Code:
@FetchProfiles({
@FetchProfile(name = "detail", fetchOverrides = {
@FetchProfile.FetchOverride(entity = Customer.class, association = "orders", mode = FetchMode.JOIN),
@FetchProfile.FetchOverride(entity = Address.class, association = "city", mode = FetchMode.JOIN)
})
})
Customer {
@OneToMany(fetch=FetchTye.LAZY)
private Order orders;
@ManyToOne(fetch=FetchTye.EAGER)
private Address mainAddress;
}
@FetchProfiles({
@FetchProfile(name = "detail", fetchOverrides = {
@FetchProfile.FetchOverride(entity = Customer.class, association = "orders", mode = FetchMode.JOIN),
@FetchProfile.FetchOverride(entity = Customer.class, association = "mainAddress", mode = FetchMode.JOIN),
@FetchProfile.FetchOverride(entity = Consumer.class, association = "whines", mode = FetchMode.JOIN)
})
})
Consumer extends Customer{
@OneToMany(fetch=FetchTye.LAZY)
List<Complaint> whines;
}
@FetchProfiles({
@FetchProfile(name = "detail", fetchOverrides = {
@FetchProfile.FetchOverride(entity = Order.class, association = "orderLines", mode = FetchMode.JOIN)
})
})
Order{
@ManyToOne(fetch=FetchType.LAZY)
Customer
@OneToMany(fetch=FetchTye.LAZY)
private OrderLine orderLines;
}
@FetchProfiles({
@FetchProfile(name = "detail", fetchOverrides = {
@FetchProfile.FetchOverride(entity = GiftWrappedOrder.class, association = "wrappingMunchKin", mode = FetchMode.JOIN)
})
})
GiftWrappedOrder extends Order{
//We want to be able to track the wrapping Munchkin for quality purposes
@ManyToOne(fetch=FetchTye.LAZY)
MunchKin wrappingMunchKin;
}
Address
@ManyToOne(fetch=FetchTye.LAZY)
private City city;
1. FetchProfile for GiftWrappedOrder. (hierarchy)
How do you specify a fetchprofile that will assure that the wrappingMuchKin will be eagerly joined when "detail" is active?
2. FetchProfile for City (deeper associations)
How do you specify a fetchprofile that will assure that cities will be eagerly joined when orders are fetched?
Basically, fetchprofiles work for me, but I have problems in the above mentioned cases.
I can't seem to be able to change the FetchMode for deeper associations and entities that are children of entities with active FetchProfiles themselves. And I'm also not sure about the placement of FetchProfile annotations.
Any suggestions are welcome.
Kind regards,
Marc