Hi,
This is a pretty big problem that I'd like to figure out a good solution for. I have a base entity class that all my entities extend, this contains a boolean column: deleted.
Instead of hard deleting a row from the database, I set this flag to true, and always filter it out through queries. In findAll(), i use a criteria:
Code:
public List<Entity> readAll(boolean includeDeleted) {
Criteria criteria = getSession().createCriteria(getDomainClass());
if (!includeDeleted) {
criteria.add(Expression.or(Expression.isNull("deleted"), Expression
.eq("deleted", false)));
}
return criteria.list();
}
that works pretty well. Now for my named queries, I
always have to remember to put:
Code:
@NamedQuery(name="User.findByLoginName",
query="FROM User WHERE loginName = ? AND deleted = false")
If i forget that deleted = false, i get rows back that shouldn't exist (the reasons for keeping them are good so I don't want to change that.)
I'm wondering if there's a way to dynamically add that to the query at runtime. There's a few ways to approach this, so no good one looks possible, and no possible one looks good.
Can I convert the query to a criteria and use .add() ? Can I subclass query or some component there of and add that parameter? Do I need to do a subselect? What is ResultTransformer? I'm worried about the performace of those two. As a last resort I could try stuffing deleted = false into the sql string but that could be very error prone for a lot of cases. Unless I use a parser on the query which I'd rather not get into.
Thoughts?