-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Adding a common NamedParameter to every Query that runs?
PostPosted: Mon Apr 02, 2007 3:56 pm 
Beginner
Beginner

Joined: Sat Aug 19, 2006 8:04 pm
Posts: 30
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?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 02, 2007 4:56 pm 
Beginner
Beginner

Joined: Sat Aug 19, 2006 8:04 pm
Posts: 30
well this might work, if i could figure out how to get the syntax right.

Code:
Query newQuery = getSession().createQuery("FROM (" + theQuery.getQueryString() + ") AS results WHERE results.deleted = false");


If i translate that to SQL it works great, but the HQL parser doesn't like the FROM ( part ... any ideas?

TIA


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.