我无法使@FetchProfile与HQL一起工作。FetchProfiles是否支持HQL?
我用它(旧)原生Criteria和session.get()API成功使用它,但与HQL无关。
这是正常吗?
感谢 我的映射和FetchProfile定义
@FetchProfiles({ @FetchProfile(name = "countryCities",
fetchOverrides = { @FetchOverride(entity = Country.class,
association = "cities", mode = FetchMode.JOIN) }) })
public class Country implements java.io.Serializable {
@Id
private long id;
@Version
private int version;
private String country;
@OneToMany(mappedBy = "country")
private Set<City> cities = new HashSet<City>(0);
}
以下不工作:
@Override
public List<Country> findAllFetchProfileCities() {
getSession().enableFetchProfile("countryCities");
boolean enabled = getSession().isFetchProfileEnabled("countryCities");
List<Country> list = getSession().createQuery("from Country").list();
enabled = getSession().isFetchProfileEnabled("countryCities");
return list;
}
以下几项工作确定:
@Override
public Country findOneFetchProfileCities(Long _id) {
getSession().enableFetchProfile("countryCities");
Country c = (Country) getSession().get(Country.class, _id);
return c;
}
@Override
public List<Country> findAllCriteriaFetchProfileCities() {
getSession().enableFetchProfile("countryCities");
return getSession().createCriteria(Country.class).list();
}
如果我不得不在HQL中添加关键字“join”或“join fetch”,那么fetchProfile将不会有用。 FetchProfile应该改变抓取计划,所以我不必这样做。当我在Criteria和session.get()上测试它时,它工作正常。 – 2013-02-22 11:04:10