排除在Hibernate对象中加载特定属性 [英] Exclude Loading of specific properties in Hibernate object

查看:94
本文介绍了排除在Hibernate对象中加载特定属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体类

  class B {
@ManyToOne(fetch = FetchType.EAGER)
私人A a;
@ManyToOne(fetch = FetchType.LAZY)
private C c;

}



在某些情况下,I不想加载对象A,因为我已经拥有该对象。
和C也一样。但是,这些都是我不想加载这些对象的特定场景。



有什么办法可以告诉hibernate不要加载实体对象的某些属性。 {没有渴望/取得建议。我只想在特定情况下发生这种情况]



注意:我正在使用Criteria来获取对象。

HQL 作为您的查询,您可以指定您的>解决方案 docs.jboss.org/hibernate/core/3.5/reference/en/html/performance.html#performance-fetching-profilesrel =nofollow noreferrer>在运行时抓取策略根据您的情况使用 fetch关键字,如下所示:

 列表结果= sess.createCriteria(B.class)
.add(限制.like(name,yourcondition%))
.setFetchMode(a,FetchMode.EAGER)
.setFetchMode(c,FetchMode.LAZY)
.list( );

编辑:



由于FetchMode.EAGER和FetchMode.LAZY已被弃用,因此FetchMode.SELECT或FetchMode.JOIN

  List结果= sess.createCriteria(B.class)
.add(name,yourcondition%))
.setFetchMode(a,FetchMode.JOIN)
.setFetchMode(c,FetchMode.SELECT)
.list();

如果您想避免SELECT或JOIN检查


I have a entity class

class B{
    @ManyToOne(fetch = FetchType.EAGER)
    private A a;
    @ManyToOne(fetch = FetchType.LAZY)
    private C c;

}

In certain scenarios, I don't want to load object A, since I already have that object. and same for C also. But these are specific scenarios I don't want to load those objects at all.

is there any way we can tell hibernate not to load certain properties of entity object. {no Eager/Fetch suggestions. I just want this to happen only in specific cases]

Note: I am using Criteria to fetch the object right now.

解决方案

Since you're using HQL for your queries, you can specify your fetching strategy at runtime based on your condition using the "fetch" keyword, like below:

List result = sess.createCriteria(B.class)
    .add( Restrictions.like("name", "yourcondition%") )
    .setFetchMode("a", FetchMode.EAGER)
    .setFetchMode("c", FetchMode.LAZY)
    .list();

Edit:

Since FetchMode.EAGER and FetchMode.LAZY is deprecated use, FetchMode.SELECT or FetchMode.JOIN

List result = sess.createCriteria(B.class)
    .add( Restrictions.like("name", "yourcondition%") )
    .setFetchMode("a", FetchMode.JOIN)
    .setFetchMode("c", FetchMode.SELECT)
    .list();

If you want to avoid the SELECT or JOIN check here.

这篇关于排除在Hibernate对象中加载特定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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