本文介绍了Hibernate - 类级别@Where注解不在该类的集合上执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在类级注释了一个带有@Where属性的Hibernate实体。
这限制了当我直接查询它时加载的实体,但它似乎不适用于该类的集合。这是预期的吗?

I have annotated a Hibernate entity with a @Where attribute at the class level.This restricts which entities are loaded when I query it directly, but it does not seem to be applied to collections of that class. Is that to be expected?

文档不清楚:

The docs are not clear on this: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-class

这听起来像它适用于检索该类的所有对象的情况,但我观察到了这一点被忽略的集合。

That sounds to me like it applies to all cases where objects of that class are retrieved, but I have observed this being ignored for collections.


  1. 这是否适用于集合?

  2. 如果不是,是将全部过滤器应用到类的所有实例以及该类的所有集合的最佳方法?

(UPDATE:I募集了 https://hibernate.atlassian.net/browse/ )

(UPDATE: I raised https://hibernate.atlassian.net/browse/HHH-6781 to track the doc issues.)

推荐答案

@如果在类级别将省略不需要的实例当使用getAll或loadAll时。在课程级别有@Where将不会级联以适用于该实体的集合。

@Where at the class level will omit the unwanted instances when using "getAll" or "loadAll". Having @Where at the class level will not "cascade" to apply to the collections on that entity.

@在集合属性级别处
public class PuppySeller {b / b>

@Where at the collection property level public class PuppySeller {

    @Where(clause = "status='FOR_SALE'")
    public Set<Puppy> getPuppiesForSale() {}

将确保当您从数据库中选择PuppySeller时,puppiesForSale只会被具有FOR_SALE状态的小狗填充。

will ensure that when you select "PuppySeller" from the DB, "puppiesForSale" will only be populated by puppies with the "FOR_SALE" status.

然而,要小心谨慎,因为它只适用于一级集合。我的意思是说你有一只小狗,并且你想让所有的小狗由同一个卖主出售。你可能会做一些像myPuppy.getPuppySeller()。getPuppiesForSale()。然而,如果你这样做,你将最终得到所有属于puppySeller的小狗,不论其状态如何,包括所有已售出或新生儿等小狗。

However, be very cautious, because it only applies for first level collections. What I mean by this, is that say you have a puppy, and you want to get all puppies for sale by the same seller. You might do something like "myPuppy.getPuppySeller().getPuppiesForSale()". However, if you do this, you will end up with all puppies, regardless of status, that belong to the puppySeller, including all puppies that are "SOLD" or "NEWBORN", etc.

这篇关于Hibernate - 类级别@Where注解不在该类的集合上执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!