Friday, December 18, 2009

Where clause in Hibernate class mapping

I have a table with a column STATUS. There are two possible values: 1 and 0. 1 means active and 0 inactive. The data with inactive status are for archives only so I'd like to filter them out in the application. Of course, I can do this in the query, criteria or filter. But I wonder if there is a way to do this in mapping, once and for all. It turns out you can. You just need to specify the "where" attribute in class mapping. For example,

<class name="my.com.Report" table="IREPORT" where="STATUS=1">



This will include the active report in query. However, as always, there is a catch. This restriction does not work by default in association. According to a reference here, if the class is on the many side of one-to-many relationship, you need to add this "where" explicitly in the one-to-many mapping. But I could not figure out how to do this yet.