【问题标题】:Does hibernate filter plugin for grails support table join and multiple parameter?grails的hibernate过滤器插件是否支持表连接和多参数?
【发布时间】:2013-05-24 09:07:33
【问题描述】:

我正在学习使用这个插件。按照 grails.org 中的步骤,我成功地在单个域类中应用了过滤器。但我想知道休眠过滤器支持条件是否跨越两个表。我在下面列出了我的案例的详细信息。能给点建议吗?

背景: 1.我有下面的数据库架构(这两个表之间的关系是一个产品可以有几个产品项是一对多的关系。)

Table : Product
column
------------------------------
id               |  auto_increment
name             | 
create_uid       | the user id create this product

另一个表:ProductItem


id                | auto_increment
name              |
product_id        |the value specify which product it belongs to

我想要什么?

当一个用户访问数据时,我想列出他自己创建的产品项目。但只有产品表有“创建它的用户信息”。我想要的结果与执行下面的sql语句相同:

"
select product_item.id from product_item ,product where   
product_item.product_id=product.id and product.create_uid=xxx
"

由于系统已经实施。我想在不更改代码的情况下使用休眠过滤器。

我知道什么?

从文档中,我注意到休眠过滤器的定义支持“集合”来指定另一个关系。但我担心的是我找不到任何指定条件的地方(“orderItem.order_id = order.id”)

class ProductItem{

Product product
static hibernateFilters = {
   secureFilter(condition:'create_uid=5', collection:"product", default:true)   // This doen't work 
}  
}

【问题讨论】:

    标签: hibernate grails filter


    【解决方案1】:

    如果插件中有任何更新和花哨的功能来处理这个问题,我不是最新的,但我以前使用子选择来解决这种情况:

    secureFilter(condition: 'product_id in (select product.id from product where product.create_uid=5)', default: true)
    

    【讨论】:

    • wwwclaes,如你所知,我们可以在过滤器的定义中使用参数。像 secureFilter(condition:"create_uid=:myParam", types:"string")。但是在每次使用之前,我们需要指定参数的值。 filter.setParameter('myParam',5) 我想知道在编程语言中是否存在像全局变量这样的用法。在我的问题的场景中,当我在用户登录我们的系统后获得用户的 id 信息时,我可以在那里设置参数的值。对于其他地方,他们可以通过指定“default:true”直接使用。
    • 我不确定我是否完全理解你。但也许这会有所帮助:查看插件中 HibernateFilterFilters 的来源。通过复制到您的应用程序 conf-dir 来使用您自己的实现覆盖它。启用过滤器并根据需要设置参数(例如,通过从 session、springSecurityService 或类似中检索它们)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 1970-01-01
    • 2018-12-10
    • 2017-10-19
    相关资源
    最近更新 更多