-->
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.  [ 4 posts ] 
Author Message
 Post subject: Creating generic SQL query using setParameter
PostPosted: Wed Sep 29, 2010 9:54 am 
Beginner
Beginner

Joined: Wed Nov 12, 2008 12:07 pm
Posts: 21
Why can't I do this:
Code:
        Query q = em.createQuery("SELECT o FROM ?1 o WHERE o.?2 = ?3");
        q.setParameter(1, className);
        q.setParameter(2, attribute);
        q.setParameter(3, value);

???

I'm doing this:
Code:
        String query = "SELECT o FROM " + className + " o WHERE o." + attribute + " = \"" + value +"\"";

but this feels very wrong.

I just wanted to make a method for generic searches.


Top
 Profile  
 
 Post subject: Re: Creating generic SQL query using setParameter
PostPosted: Thu Sep 30, 2010 8:13 am 
Beginner
Beginner

Joined: Fri Nov 14, 2008 7:34 pm
Posts: 24
do not mix entity names ( mandatory when parsing jsql) and parameters

Query q = em.createQuery(String.format("SELECT o FROM %s o WHERE o.%s = ?1", className, attribute));
q.setParameter(1, value);


Top
 Profile  
 
 Post subject: Re: Creating generic SQL query using setParameter
PostPosted: Thu Sep 30, 2010 12:40 pm 
Newbie

Joined: Thu Jan 05, 2006 3:59 pm
Posts: 10
So, it's a wee bit evil, but you can do this:
Code:
public List<MappedClass> myGenericQueryMethod(Class<MappedClass> myClass, String propertyName, Object value)
{
  Session session = (org.hibernate.Session)em.getDelegate(); //Gives you the underlying hibernate session
  Criteria crit = session.createCriteria(myClass); // standard criteria query stuff.
  crit.add(Restrictions.eq(propertyName,value));
  return (List<MappedClass>)crit.list();
}


Kinda defeats the purpose of having an abstraction like JPA if you're just gonna grab the underlying implementation and muck with it, but what the hell? Depends on what your priorities are.

And if you're in JBoss, using EJBs, you can even do fun stuff like:

Code:
  @PersistenceContext(unitName="my-persistence") org.hibernate.Session session;


and just skip the EntityManager stuff. In my EJBs, I just inject both, and use whichever one's easiest.

Like I said . . . evil, but I'm not migrating to another provider ever, so it's not a big deal.

-Falken


Top
 Profile  
 
 Post subject: Re: Creating generic SQL query using setParameter
PostPosted: Mon Oct 04, 2010 7:21 pm 
Beginner
Beginner

Joined: Wed Nov 12, 2008 12:07 pm
Posts: 21
Thank you both.

Unfortunately I am using a JPA implementation that doesn't allow criteria, not Hibernate.

Now I am having this problem: the value field is String but I wanted to pass anything there (string, double, int...). The problem is that setParameter needs to know the actual type of value, or it will return empty when trying to search for a field that is, for example, an integer. So, the question is, is it viable to make such generic search method?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.