public List list ( Class clazz , String key , Object value ) { EntityManager entityManager = null ; try { entityManager = entityManagers . poll ( ) ; Query query = entityManager . createQuery ( "SELECT c FROM " + clazz . getCanonicalName ( ) + " c where c." + key + " = :param" ) ; query . setParameter ( "param" , value ) ; return query . getResultList ( ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } finally { if ( entityManager != null ) entityManagers . add ( entityManager ) ; } return null ; }
Execute query with criteria of key = key . This will auto generate a sql statement .