Hibernate - 类级别 @Where 注释未在该类的集合上强制执行?
我在类级别使用 @Where 属性注释了 Hibernate 实体。 这限制了当我直接查询它时加载哪些实体,但它似乎不适用于该类的集合。这是可以预料的吗?
文档对此并不清楚: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html#mapping-declaration-class
where(可选):指定任意 SQL WHERE 条件 检索此类的对象时使用。
在我看来,它适用于检索该类的对象的所有情况,但我观察到这一点在集合中被忽略。
- 这是否适用于收藏?
- 如果不是,那么将过滤器全局应用到类的所有实例以及该类的所有集合的最佳方法是什么?
(更新:我提出了https://hibernate.atlassian.net/browse/HHH-6781 跟踪文档问题。)
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
where (optional): specifies an arbitrary SQL WHERE condition to be
used when retrieving objects of this 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.
- Is this meant to work for collections?
- If not, what is the best way to globally apply a filter to all instances of a class, and all collections of that class?
(UPDATE: I raised https://hibernate.atlassian.net/browse/HHH-6781 to track the doc issues.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当使用“getAll”或“loadAll”时,类级别的 @Where 将省略不需要的实例。在类级别拥有 @Where 不会“级联”以应用于该实体上的集合。
@Where 在集合属性级别
public class PuppySeller {
将确保当您从数据库中选择“PuppySeller”时,“puppiesForSale”将仅由状态为“FOR_SALE”的小狗填充。
但是,要非常谨慎,因为它仅适用于第一级收藏。我的意思是,假设您有一只小狗,并且您希望同一卖家出售所有小狗。您可能会执行类似“myPuppy.getPuppySeller().getPuppiesForSale()”的操作。但是,如果您这样做,您最终将获得属于 puppySeller 的所有小狗,无论其状态如何,包括所有“已出售”或“新生”等的小狗。
@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.
@Where at the collection property level
public class PuppySeller {
will ensure that when you select "PuppySeller" from the DB, "puppiesForSale" will only be populated by puppies with the "FOR_SALE" status.
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.