如何用Hibernate Criteria懒散地获取字段 [英] How to fetch a field lazily with Hibernate Criteria

查看:96
本文介绍了如何用Hibernate Criteria懒散地获取字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表的每一行 Person (具有名称名字
$ $ p $ age > EntityManager em = emf.createEntityManager();
Session s =(Session)em.getDelegate();
Criteria criteria = s.createCriteria(Person.class);
criteria.setFetchMode(age,FetchMode.SELECT);

但SQL显示

  Hibernate:
选择
person0_.name,
person0_.firstname,
person0_.age
from
SCOPE.PERSON person0_

如何让年龄延迟 ONLY / p>

解决方案

我认为懒惰模式只对关联有意义。如果您访问的是普通表格,则会加载所有字段。



如果您希望 age 字段不是出现在SQL中,因此不会被加载到内存中,然后使用投影:

pre $ Criteria crit = session.createCriteria(Person.class) ;
ProjectionList projList = Projections.projectionList();
projList.add(Projections.property(name));
projList.add(Projections.property(firstname));
crit.setProjection(projList);


Each row of the table Person (having name, firstname and age) shall be read.

EntityManager em = emf.createEntityManager();
Session s = (Session) em.getDelegate();
Criteria criteria = s.createCriteria(Person.class);
criteria.setFetchMode("age", FetchMode.SELECT);

But the SQL shows

Hibernate:
    select
        person0_.name,
        person0_.firstname,
        person0_.age
    from 
        SCOPE.PERSON person0_

How to let the age be lazy ONLY for the Criteria??

解决方案

I think that lazy mode only makes sense with associations. If you are accessing a plain table it will load all the fields.

If you want the age field not to appear in the SQL and so not being loaded into memory then use projections:

Criteria crit = session.createCriteria(Person.class);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.property("name"));
projList.add(Projections.property("firstname"));
crit.setProjection(projList);

这篇关于如何用Hibernate Criteria懒散地获取字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆