将@Filter与@ManyToOne关系一起使用 [英] Using @Filter with @ManyToOne relation

查看:173
本文介绍了将@Filter与@ManyToOne关系一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Hibernate 3.6.7-Final和Spring 3.0.5。

我有这样的实体

  @Entity 
公共类Foo {
@ManyToOne
私人酒吧;
}

@实体
公共类Bar {
@Column
字符串组;

如何使用 @Filter 在Foo中,我希望所有 Foo Bar 与 group =:group ?这应该是一个安全约束。



试着设置 @Filter(name =groupFilter,condition =group =:group )在属性 bar Foo 但它不起作用。 hibernate是否支持这个或者Filter只能在实体/集合级别工作?我必须改变所有的HQL来添加这个安全约束吗?

首先,你必须在某处创建@FilterDef(这定义了可用参数和默认条件),然后在特定的类上定义@Filter。

最后,必须启用Session对象上的过滤器,并且设置它需要的参数。过滤器默认情况下未在休眠会话中启用。您必须在打开会话后启用特定的功能。



请参阅第19.1节: http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/ filters.html

  @FilterDef(name =groupFilter,parameters = {@ ParamDef(name =group ),type =string)})
@Filters(
{@Filter(name =groupFilter,condition =group =:group)}

public class ... {
}

然后在你的dao代码中的某处:

  session.enableFilter(groupFilter)。setParameter(group,group); 

您不必触碰任何hql。当你启用过滤器时,所有为其定义的实际@Filter的类将自动应用该条件。



还有其他方法可用于集合,而我建议你阅读上面提到的文档。但通常,您可以提供@Filter注记类和集合属性。


I'm using Hibernate 3.6.7-Final and Spring 3.0.5.

I have entity like this

@Entity
public class Foo {
    @ManyToOne
    private Bar bar;
}

@Entity
public class Bar {
    @Column
    String group;
}

How can I use @Filter in Foo where I want to get all Foo's that have Bar with group = :group? This is supposed to be a security constraint.

Tryied just setting @Filter(name = "groupFilter", condition = "group = :group") at the attribute bar fromFoo but it didn't work. Does hibernate have support for this or Filter only works at entity/collection level? Will I have to change all my HQLs to add this security constraint?

解决方案

First, you have to create the @FilterDef somewhere (this defines the available parameters, and the default condition), then define the @Filter on a particular class.

Finally, have to enable the filter on the Session object, and set any parameters it requires. Filters are not enabled in hibernate sessions by default. You have to enable the specific ones you want once the session is opened.

See section 19.1 for an example: http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/filters.html

@FilterDef(name="groupFilter", parameters={@ParamDef( name="group", type="string" )})
@Filters(
  { @Filter(name="groupFilter", condition="group = :group") }
)
public class ... {
}

Then somewhere in your dao code:

session.enableFilter("groupFilter").setParameter("group", group);

You should not have to touch any hql. Whenn you enable the filter, all classes which have an actual @Filter defined for it will automatically apply the condition.

There are additional ways to do things for collections, and i suggest you read the documentation referenced above for that. But in general, you can provide the @Filter annotation classes and on collection properties.

这篇关于将@Filter与@ManyToOne关系一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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