コード例 #1
0
 @SuppressWarnings("unchecked")
 private <T> T queryStringWithParameters(
     Map<String, Object> params,
     boolean singleResult,
     LockModeType lockMode,
     Class<T> clazz,
     Query query) {
   ;
   if (lockMode != null) {
     query.setLockMode(lockMode);
   }
   if (params != null && !params.isEmpty()) {
     for (String name : params.keySet()) {
       if (FIRST_RESULT.equals(name)) {
         query.setFirstResult((Integer) params.get(name));
         continue;
       }
       if (MAX_RESULTS.equals(name)) {
         query.setMaxResults((Integer) params.get(name));
         continue;
       }
       if (FLUSH_MODE.equals(name)) {
         query.setFlushMode(FlushModeType.valueOf((String) params.get(name)));
         continue;
       }
       query.setParameter(name, params.get(name));
     }
   }
   if (singleResult) {
     return (T) query.getSingleResult();
   }
   return (T) query.getResultList();
 }
 @SuppressWarnings({"unused", "unchecked"})
 public void testQueryNoPersonsShared() {
   EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(entityManagerFactory);
   Query q = em.createQuery("select p from Person as p");
   q.setFlushMode(FlushModeType.AUTO);
   List<Person> people = q.getResultList();
   try {
     assertNull(q.getSingleResult());
     fail("Should have thrown NoResultException");
   } catch (NoResultException ex) {
     // expected
   }
 }
 @NotTransactional
 @SuppressWarnings("unchecked")
 public void testQueryNoPersonsSharedNotTransactional() {
   EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(entityManagerFactory);
   Query q = em.createQuery("select p from Person as p");
   q.setFlushMode(FlushModeType.AUTO);
   List<Person> people = q.getResultList();
   assertEquals(0, people.size());
   try {
     assertNull(q.getSingleResult());
     fail("Should have thrown IllegalStateException");
   } catch (Exception ex) {
     // IllegalStateException expected, but PersistenceException thrown by Hibernate
     assertTrue(ex.getMessage().indexOf("closed") != -1);
   }
   q = em.createQuery("select p from Person as p");
   q.setFlushMode(FlushModeType.AUTO);
   try {
     assertNull(q.getSingleResult());
     fail("Should have thrown NoResultException");
   } catch (NoResultException ex) {
     // expected
   }
 }