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