Force eager loading of otherwise lazy loaded prope

2019-03-22 17:34发布

I've got a Hibernate object which's properties are all loaded lazy. Most of these properties are other Hibernate objects or PersistentSets.

Now I want to force Hibernate to eager load these properties for just one time.

Of course I could "touch" each of these properties with object.getSite().size() but maybe there's another way to achieve my goal.

7条回答
来,给爷笑一个
2楼-- · 2019-03-22 17:43

The documentation puts it like this:

You can force the usual eager fetching of properties using fetch all properties in HQL.

References

查看更多
Luminary・发光体
3楼-- · 2019-03-22 17:44

This is an old question, but I also wanted to point out the static method Hibernate.initialize.

Example usage:

Person p = sess.createCriteria(Person.class, id);
Hibernate.initialize(p.getChildren());

The children are now initialized to be used even after the session is closed.

查看更多
我命由我不由天
4楼-- · 2019-03-22 17:45

This is slow, because it makes a round trip for every item it needs to initialize, but it gets the job done.

private void RecursiveInitialize(object o,IList completed)
{
    if (completed == null) throw new ArgumentNullException("completed");

    if (o == null) return;            

    if (completed.Contains(o)) return;            

    NHibernateUtil.Initialize(o);

    completed.Add(o);

    var type = NHibernateUtil.GetClass(o);

    if (type.IsSealed) return;

    foreach (var prop in type.GetProperties())
    {
        if (prop.PropertyType.IsArray)
        {
            var result = prop.GetValue(o, null) as IEnumerable;
            if (result == null) return;
            foreach (var item in result)
            {
                RecursiveInitialize(item, completed);
            }
        }
        else if (prop.PropertyType.GetGenericArguments().Length > 0)
        {
            var result = prop.GetValue(o, null) as IEnumerable;
            if (result == null) return;
            foreach (var item in result)
            {
                RecursiveInitialize(item, completed);
            }
        }
        else
        {
            var value = prop.GetValue(o, null);
            RecursiveInitialize(value, completed);
        }
    }
}
查看更多
你好瞎i
5楼-- · 2019-03-22 17:50

According to the hibernate docs, you should be able to disable lazy property loading by setting the lazy attribute on your particular property mappings:

<class name="Document">
  <id name="id">
    <generator class="native"/>
  </id>
  <property length="50" name="name" not-null="true"/>
  <property lazy="false" length="200" name="summary" not-null="true"/>
  <property lazy="false" length="2000" name="text" not-null="true"/>
</class>
查看更多
萌系小妹纸
6楼-- · 2019-03-22 17:57

For me this works:

Person p = (Parent) sess.get(Person.class, id);
Hibernate.initialize(p.getChildren());

Rather than this:

Person p = sess.createCriteria(Person.class, id);
Hibernate.initialize(p.getChildren());
查看更多
时光不老,我们不散
7楼-- · 2019-03-22 17:59

3 ways

1.HQL with left join children

2.SetFetchMode after createCriteria

3.Hibernate.initialize

查看更多
登录 后发表回答