How to fetch a field lazily with Hibernate Criteria(如何使用 Hibernate Criteria 懒惰地获取字段)
问题描述
应读取表Person(具有name、firstname和age)的每一行.
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);
但是 SQL 显示
Hibernate:
    select
        person0_.name,
        person0_.firstname,
        person0_.age
    from 
        SCOPE.PERSON person0_
如何让年龄变得懒惰ONLY为Criteria??
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.
如果您希望 age 字段不出现在 SQL 中,因此不被加载到内存中,请使用投影:
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 懒惰地获取字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 Hibernate Criteria 懒惰地获取字段
				
        
 
            
        基础教程推荐
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
 - Java Swing计时器未清除 2022-01-01
 - Java 实例变量在两个语句中声明和初始化 2022-01-01
 - 不推荐使用 Api 注释的描述 2022-01-01
 - 验证是否调用了所有 getter 方法 2022-01-01
 - 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
 - 多个组件的复杂布局 2022-01-01
 - 从 python 访问 JVM 2022-01-01
 - 在 Java 中创建日期的正确方法是什么? 2022-01-01
 - 大摇大摆的枚举 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
						
						
						
						
						
				
				
				
				