Hello, I have a question regarding hibernate annotation & criteria API.
My hibernate class looks like this (pseudocode):
Code:
public class A{
....
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name="reporting_year_id", referencedColumnName="reporting_year_id", updatable=false, insertable = false,columnDefinition="number(10,0)")
@ForeignKey(name = "ctl_activity_reporting_year_fk")
protected B b;
}
public class B{
.....
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@Cascade({org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
@JoinColumn(name = "reporting_year_id", referencedColumnName = "reporting_year_id")
@ForeignKey(name = "bus_cycle_reporting_year_fk")
protected List<C> c;
}
public class C{
....
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@Cascade({org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
@ForeignKey(name = "mgmt_unit_bus_cycle_fk")
@JoinColumn(name = "business_cycle_id", referencedColumnName = "business_cycle_id")
protected List<D> d;
}
I perform the criteria on A.
Code:
Criteria criteria = session.createCriteria(A.class);
if( ... some condition...){
Criteria bCriteria = criteria.createCriteria("b");
bCriteria .add(Restrictions.eq("reportingYearID", req.getReportingYearID()));
Criteria cCriteria = bCriteria .createCriteria("businessCycle");
cCriteria .add(Restrictions.in("businessCycleID", req.getBusinessCycleID()));
}
I dont want the query on A (i.e. the criteria above) to fetch B, C, D, and so on. But as soon as I specify a criteria on B (the if condition above), then hibernate will do the join...(i saw this on log4j output).
I still need the EAGER fetch type on B and C.
I tried to specify setFetchMode() on the association to be LAZY, but it didnt have any effect.
Is there any way to override this annotation-driven fetch strategy?
Thanks a lot!
Alex.