Common criteria restriction in Hibernate for all queries for all tables

﹥>﹥吖頭↗ 提交于 2020-01-03 02:25:17

问题


I have already finished writing a lot of queries in hibernate for my project. Now my requirement has changed and a new column name ACTIVE is added in all the tables I have in MySql. I have no permission to deny the addition of ACTIVE column. The ACTIVE field can hold values true or false and I have to add that to the criteria too. So, I have been thinking is there a way I could add a criteria restriction in Hibernate that is kind of common to all the queries I make, instead of putting that criteria for every query I make?

EDIT: Little misunderstanding I suppose, sorry for that, I was in hurry while typing this. The ACTIVE column will be already set to true or false by some other user. What I need to is a global filtering only for the records having ACTIVE column as true for every query I fire.

Something like this but I want hibernate to add the the ACTIVE='true' filter for every query I fire,

select name from table where <other-conditions-here> and ACTIVE='true'

Hope that make sense.

Thanks and regards,

Sunayan Saikia


回答1:


You can use hibernate filters for this purpose. for details refer doc




回答2:


I would advice you to implement a custom interceptor, Which should intercept your queries and append ACTIVE criteria before they are executed.

Interceptor details could be found here: Hibernate Interceptors



来源:https://stackoverflow.com/questions/13448524/common-criteria-restriction-in-hibernate-for-all-queries-for-all-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!