本文介绍了带参数的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 子句过滤豆子(产品),我想要什么,以便一个用户只能看到来自其自己公司的产品.公司 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.

推荐答案

hibernate 中有很多方法可以做到这一点,

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/

You can get more information from this link http://www.mkyong.com/hibernate/hibernate-parameter-binding-examples/

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

10-15 16:53