简体   繁体   English

Hibernate对基本实体的manyToOne过滤器

[英]Hibernate manyToOne filter on base entity

I have an entity "Event" that has a ManyToOne relationship with the entity "Organization". 我有一个实体“事件”,它与实体“组织”有一个ManyToOne关系。 So an Organization can have multiple events. 因此,组织可以有多个事件。

What I originally wanted to do was to filter the entity Event using a property of the Organization entity. 我最初想要做的是使用Organization实体的属性过滤实体Event。 So basically when I fetch events, only return the events that have an Organization.code= :codeParam. 所以基本上当我获取事件时,只返回具有Organization.code =:codeParam的事件。

To accomplish that I implemented a hibernate filter with : 为此,我实现了一个hibernate过滤器:

@FilterDef(name="codeFilter", parameters=@ParamDef( name="codeParam", type="string" ) )

... ...

@ManyToOne
@JoinColumn(name="Organization_Id")
@Filter(name="codeFilter", condition=" code = :codeParam")
private Organization organization;

... ...

Filter hibernateFilter = sess.enableFilter("codeFilter");
hibernateFilter.setParameter("codeParam", "hola");

Unfortunately according to a post from the Hibernate Team on the hibernate forums, this is not possible : 不幸的是根据hibernate论坛上Hibernate团队的帖子,这是不可能的:

A Hibernate data filter does not change the multiplicity of an association. Hibernate数据过滤器不会更改关联的多样性。 By definition it therefore does not filter many-to-one, one-to-one, or any load() or get() operation. 因此,根据定义,它不会过滤多对一,一对一或任何load()或get()操作。

What is it supposed to do, return NULL instead of an instance? 它应该做什么,返回NULL而不是实例? NULL does not mean FILTERED, it means NULL. NULL并不意味着FILTERED,它意味着NULL。 You guys are using filters wrong. 你们使用过滤器错了。

So my question is : is there any way to filter the base entity ("Event") with a condition on the entity from a manyToOne relationship (Organization.code= :codeParam)? 所以我的问题是:有没有办法从manyToOne关系(Organization.code =:codeParam)过滤实体上的条件的基本实体(“事件”)?

I need this to be enforced every time there is a fetch of events, so a solution using the already existing hibernate filters or something similar would be greatly appreciated. 我需要在每次获取事件时强制执行此操作,因此使用已有的hibernate过滤器或类似的东西的解决方案将非常受欢迎。

EDIT1: The question is a simple example of what needs to be done on a significantly bigger scale. 编辑1:问题是一个简单的例子,说明需要在更大规模上做些什么。 Basically, we want to add security to all our Entities and their own nested Entities through the use of a globally defined filter on a Unix permissions row that all our tables have. 基本上,我们希望通过在我们所有表所具有的Unix权限行上使用全局定义的过滤器来为所有实体及其自己的嵌套实体添加安全性。

WARNING: Do not do this, it is dependent on Hibernate internals and prone to breaking on schema changes, and possibly on variations in individual query setup. 警告:不要这样做,它依赖于Hibernate内部,并且容易打破架构更改,并且可能会出现个别查询设置的变化。

Set Hibernate to show its generated sql, run the query you want to filter (in this case, loading some Event objects), and check what name it assigns to the join used for fetching the related Organization . 设置Hibernate以显示其生成的sql,运行要过滤的查询(在这种情况下,加载一些Event对象),并检查它分配给用于获取相关Organization的连接的名称。 For example, the generated sql might include inner join Organization someNameHere on this_.Organization_Id = someNameHere.OrganizationId . 例如,生成的sql可能包含inner join Organization someNameHere on this_.Organization_Id = someNameHere.OrganizationId Then apply the filter, not to the association, but to the Event class, with condition "someNameHere.code = :codeParam" . 然后应用过滤器,而不是关联,但应用于Event类,条件为"someNameHere.code = :codeParam"

This is, unfortunately, the only way I've been able to find to filter one class by the properties of an associated class. 遗憾的是,这是我能够找到通过关联类的属性过滤一个类的唯一方法。

I'm trying to make a more robust solution, but it's a complex issue and I'm still in the research stage for that. 我正在尝试制定更强大的解决方案,但这是一个复杂的问题,我仍处于研究阶段。 I expect it will use code generation (through an annotation processor) and programmatic modification of Hibernate's mapping information on startup, but I'm not sure what else yet. 我希望它会在启动时使用代码生成(通过注释处理器)和编程修改Hibernate的映射信息,但我还不确定还有什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM