Hibernate FetchProfiles与HQL一起工作吗?

Hibernate FetchProfiles与HQL一起工作吗?,hibernate,Hibernate,我无法使@FetchProfile与HQL一起工作 我成功地将它用于(旧的)本机条件和session.get()API,但不用于HQL 这正常吗 谢谢 我的映射和配置文件定义 @FetchProfiles({ @FetchProfile( name = "countryCities", fetchOverrides = { @FetchOverride(entity = Country.class, associat

我无法使@FetchProfile与HQL一起工作

我成功地将它用于(旧的)本机条件和session.get()API,但不用于HQL

这正常吗

谢谢 我的映射和配置文件定义

    @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);
    }   
@FetchProfiles({@FetchProfile(name=“countryCities”,
fetchOverrides={@FetchOverride(entity=Country.class,
association=“cities”,mode=FetchMode.JOIN)})
公共类Country实现java.io.Serializable{
@身份证
私人长id;
@版本
私有int版本;
私人国家;
@OneToMany(mappedBy=“国家”)
私有集城市=新哈希集(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;
}
@覆盖
公共列表findAllFetchProfileCities(){
getSession().enableFetchProfile(“countryCities”);
boolean enabled=getSession().isFetchProfileEnabled(“countryCities”);
List List=getSession().createQuery(“来自国家”).List();
enabled=getSession().isFetchProfileEnabled(“countryCities”);
退货清单;
}
以下工作正常:

@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();
}
@覆盖
公共国家/地区FindNefetchProfileCities(长id){
getSession().enableFetchProfile(“countryCities”);
Country c=(Country)getSession().get(Country.class,_id);
返回c;
}
@凌驾
公共列表findAllCriteriaFetchProfileCities(){
getSession().enableFetchProfile(“countryCities”);
返回getSession().createCriteria(Country.class).list();
}

hibernate
Fetch
策略在Criteria API(Criteria.setFetchMode())和HQL(“Fetch JOIN”)中都可用

因此,当我们编写hql时,我们必须在查询中提到关键字“JOIN”It self

有点老了,但请参考一下


hibernate
Fetch
策略在Criteria API(Criteria.setFetchMode())和HQL(“Fetch JOIN”)中都可用

因此,当我们编写hql时,我们必须在查询中提到关键字“JOIN”It self

有点老了,但请参考一下


我也一直在寻找这个问题的解决方案,并找到了我认为有用的解决方法(不是说这是有效的或任何东西!)

它包括两个步骤:

首先将级联刷新添加到映射:

@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", cascade = {CascadeType.REFRESH})
    private Set<City> cities = new HashSet<City>(0);
}   
@FetchProfiles({@FetchProfile(name=“countryCities”,
fetchOverrides={@FetchOverride(entity=Country.class,
association=“cities”,mode=FetchMode.JOIN)})
公共类Country实现java.io.Serializable{
@身份证
私人长id;
@版本
私有int版本;
私人国家;
@OneToMany(mappedBy=“country”,cascade={CascadeType.REFRESH})
私有集城市=新哈希集(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");

    for (Country c:list) {
        getSession().refresh(c);
    }

    return list;
}
@覆盖
公共列表findAllFetchProfileCities(){
getSession().enableFetchProfile(“countryCities”);
boolean enabled=getSession().isFetchProfileEnabled(“countryCities”);
List List=getSession().createQuery(“来自国家”).List();
enabled=getSession().isFetchProfileEnabled(“countryCities”);
(c国:名单){
getSession().refresh(c);
}
退货清单;
}

这很难看,但它让我走得更远了。

我也一直在寻找这个问题的解决方案,并找到了我认为有用的解决方法(不是说这是有效的或任何东西!)

它包括两个步骤:

首先将级联刷新添加到映射:

@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", cascade = {CascadeType.REFRESH})
    private Set<City> cities = new HashSet<City>(0);
}   
@FetchProfiles({@FetchProfile(name=“countryCities”,
fetchOverrides={@FetchOverride(entity=Country.class,
association=“cities”,mode=FetchMode.JOIN)})
公共类Country实现java.io.Serializable{
@身份证
私人长id;
@版本
私有int版本;
私人国家;
@OneToMany(mappedBy=“country”,cascade={CascadeType.REFRESH})
私有集城市=新哈希集(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");

    for (Country c:list) {
        getSession().refresh(c);
    }

    return list;
}
@覆盖
公共列表findAllFetchProfileCities(){
getSession().enableFetchProfile(“countryCities”);
boolean enabled=getSession().isFetchProfileEnabled(“countryCities”);
List List=getSession().createQuery(“来自国家”).List();
enabled=getSession().isFetchProfileEnabled(“countryCities”);
(c国:名单){
getSession().refresh(c);
}
退货清单;
}
这很难看,但它让我走得更远了。

如果我必须在HQL中添加关键字“join”或“join fetch”,fetchProfile将不会有用。FetchProfile应该更改提取计划,这样我就不必更改了。当我在条件和session.get()上测试它时,它工作得非常好。如果我必须在HQL中添加关键字“join”或“join fetch”,fetchProfile将不会有用。FetchProfile应该更改提取计划,这样我就不必更改了。当我在标准和session.get()上测试它时,它可以完美地工作。