Hibernate注释@Where带有参数 [英] Hibernate annotation @Where with parameter

查看:355
本文介绍了Hibernate注释@Where带有参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有此类产品,以这种方式注释(我正在使用软删除):

I currently have this class Product, annotated this way (I´m using soft delete):

@SQLDelete(sql = "UPDATE products SET active = '0' WHERE id_product = ? and last_modification_date = ?")
@Where(clause = "active = '1'" )
@Entity
@Table(name = "products ")
public class Product {
  .....
}

如果要使用@Where子句过滤Bean(产品),那么我想要什么,这样一个用户只能看到它自己公司的产品.公司ID位于会话中,并且由于@SQLDelete接收参数,因此我想执行以下操作:

What I want if to FILTER the beans (products), using the @Where clause, so that one user can only see the products from it own company. The company ID is in the session, and since the @SQLDelete receives parameters, I wanted to do something like:

@Where(clause = "active = '1' and id_company = ?" )

以便我可以过滤所有表以根据每个用户获取结果,而不必编辑系统中的所有查询.有什么办法可以做到?

so that i can filter ALL the tables to fetch results according each user, without having to edit all the queries in the system. Is there any way to accomplish this?

任何帮助都会非常感激.
另外,请让我知道是否需要更多信息来理解问题.

Any help will be very aprecciated.
Also, let me know if more info is needed to understand the problem.

推荐答案

在休眠状态下有很多方法,

There are many ways of doing this in hibernate,

String hql = "from Stock s where s.stockCode = :stockCode";
List result = session.createQuery(hql)
.setString("stockCode", "7277")
.list();

您可以从此链接 http://www.mkyong .com/hibernate/hibernate-parameter-binding-examples/

这篇关于Hibernate注释@Where带有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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