public List executeNamedQuery(final String queryName, final Object[] params, final int begin, final int max) { List ret = (List) this.getJpaTemplate().execute(new JpaCallback() { public Object doInJpa(EntityManager em) throws PersistenceException { Query query = em.createNamedQuery(queryName); int parameterIndex = 1; if (params != null && params.length > 0) { for (Object obj : params) { query.setParameter(parameterIndex++, obj); } } if (begin >= 0 && max > 0) { query.setFirstResult(begin); query.setMaxResults(max); } return query.getResultList(); } }); if (ret != null && ret.size() >= 0) { return ret; } else { return new ArrayList(); } }
public List executeNativeNamedQuery(final String nnq,final Object[] params) { Object ret = this.getJpaTemplate().execute(new JpaCallback() { public Object doInJpa(EntityManager em) throws PersistenceException { Query query = em.createNamedQuery(nnq); int parameterIndex = 1; if (params != null && params.length > 0) { for (Object obj : params) { query.setParameter(parameterIndex++, obj); } } return query.getResultList(); } }); return (List) ret; }
public List executeNativeQuery(final String nnq, final Object[] params, final int begin, final int max) { List ret = (List) this.getJpaTemplate().execute(new JpaCallback() { public Object doInJpa(EntityManager em) throws PersistenceException { Query query = em.createNativeQuery(nnq); int parameterIndex = 1; if (params != null && params.length > 0) { for (Object obj : params) { query.setParameter(parameterIndex++, obj); } } if (begin >= 0 && max > 0) { query.setFirstResult(begin); query.setMaxResults(max); } return query.getResultList(); } }); if (ret != null && ret.size() >= 0) { return ret; } else { return new ArrayList(); } }
@SuppressWarnings("unchecked") public List<Client> find(final String scope, final Object[] params, final int begin, final int max) { return this.getJpaTemplate().executeFind(new JpaCallback() { public Object doInJpa(EntityManager entitymanager) throws PersistenceException { StringBuffer sql = new StringBuffer("select obj from Client obj "); if (StringUtils.hasLength(scope)) { sql.append(" where ").append(scope); } Query query = entitymanager.createQuery(sql.toString()); if (params != null) { for (int i = 0; i < params.length; i++) query.setParameter(i + 1, params[i]); } if (begin > 0) { query.setFirstResult(begin); query.setMaxResults(max); } return query.getResultList(); } }); }
public List<Note> findAll(final int offset, final int count, final String sortProperty, final boolean sortAscending) { return getJpaTemplate().execute(new JpaCallback<List<Note>>() { public List<Note> doInJpa(EntityManager em) throws PersistenceException { /* breaks with user.name sorting.....*/ // CriteriaBuilder builder = em.getCriteriaBuilder(); // CriteriaQuery<Note> select = builder.createQuery(Note.class); // Root<Note> note = select.from(Note.class); // Path<Object> sort = note.get(sortProperty); // Order order = null; // if (sortAscending) { // order = builder.asc(sort); // } else { // order = builder.desc(sort); // } // select.orderBy(order); // // TypedQuery<Note> query = em.createQuery(select); /* workaround for user.name sorting... */ TypedQuery<Note> query = em.createQuery( "from Note order by " + sortProperty + " " + (sortAscending ? "asc" : "desc"), Note.class); query.setFirstResult(offset); query.setMaxResults(count); return query.getResultList(); } }); }
@TransactionAttribute(TransactionAttributeType.SUPPORTS) public Set<T> findAll() { return getJpaTemplate().execute(new JpaCallback<Set<T>>() { public Set<T> doInJpa(EntityManager em) throws PersistenceException { TypedQuery<T> query = em.createQuery( "select x from " + type.getName() + " x", type); return new HashSet<T>(query.getResultList()); } }); }
@TransactionAttribute(TransactionAttributeType.SUPPORTS) public long countAll() { return getJpaTemplate().execute(new JpaCallback<Integer>() { public Integer doInJpa(EntityManager em) throws PersistenceException { TypedQuery<Long> query = em.createQuery("select count(x) from " + type.getName() + " x", Long.class); return (query.getSingleResult()).intValue(); } }); }
@TransactionAttribute(TransactionAttributeType.SUPPORTS) public User findByUsername(final String username) { return getJpaTemplate().execute(new JpaCallback<User>() { public User doInJpa(EntityManager em) throws PersistenceException { TypedQuery<User> query = em.createNamedQuery( "User.findByUsername", User.class); query.setParameter("name", username); try { return query.getSingleResult(); } catch (NoResultException e) { return null; } } }); }
public List<T> find(final String queryStr, final Object[] params, final int begin, final int max) { List<T> ret = (List<T>) this.getJpaTemplate().execute( new JpaCallback() { public Object doInJpa(EntityManager em) throws PersistenceException { String clazzName = clazz.getName(); StringBuffer sb = new StringBuffer("select obj from "); sb.append(clazzName).append(" obj").append(" where ") .append(queryStr); Query query = em.createQuery(sb.toString()); int parameterIndex = 1; if (params != null && params.length > 0) { for (Object obj : params) { query.setParameter(parameterIndex++, obj); } } if (begin >= 0 && max > 0) { query.setFirstResult(begin); query.setMaxResults(max); } if (begin >= 0 && max > 0) { query.setFirstResult(begin); query.setMaxResults(max); } return query.getResultList(); } }); if (ret != null && ret.size() >= 0) { return ret; } else { return new ArrayList<T>(); } }
public T getBy(final String propertyName, final Object value) { if (propertyName == null || "".equals(propertyName) || value == null) throw new IllegalArgumentException( I18n .getLocaleMessage("ext.Call.parameter.is.not.correct.attribute.names.and.values.are.not.empty")); List<T> ret = (List<T>) this.getJpaTemplate().execute( new JpaCallback() { public Object doInJpa(EntityManager em) throws PersistenceException { String clazzName = clazz.getName(); StringBuffer sb = new StringBuffer("select obj from "); sb.append(clazzName).append(" obj"); Query query = null; if (propertyName != null && value != null) { sb.append(" where obj.").append(propertyName) .append(" = :value"); query = em.createQuery(sb.toString()).setParameter( "value", value); } else { query = em.createQuery(sb.toString()); } return query.getResultList(); } }); if (ret != null && ret.size() == 1) { return ret.get(0); } else if (ret != null && ret.size() > 1) { throw new java.lang.IllegalStateException( "worning --more than one object find!!"); } else { return null; } }
public List executeNativeNamedQuery(final String nnq) { Object ret = this.getJpaTemplate().execute(new JpaCallback() { public Object doInJpa(EntityManager em) throws PersistenceException { Query query = em.createNamedQuery(nnq); return query.getResultList(); } }); return (List) ret; }
public int executeNativeSQL(final String nnq) { Object ret = this.getJpaTemplate().execute(new JpaCallback() { public Object doInJpa(EntityManager em) throws PersistenceException { Query query = em.createNativeQuery(nnq); return query.executeUpdate(); } }); return (Integer) ret; }
public int batchUpdate(final String jpql, final Object[] params) { Object ret = this.getJpaTemplate().execute(new JpaCallback() { public Object doInJpa(EntityManager em) throws PersistenceException { Query query = em.createQuery(jpql); int parameterIndex = 1; if (params != null && params.length > 0) { for (Object obj : params) { query.setParameter(parameterIndex++, obj); } } return query.executeUpdate(); } }); return (Integer) ret; }
public void flush() { this.getJpaTemplate().execute(new JpaCallback() { public Object doInJpa(EntityManager em) throws PersistenceException { Session session = (Session) em.getDelegate(); session.flush(); return null; } }); }
@SuppressWarnings("unchecked") @Transactional(readOnly = true) public List<T> findAll() { return (List<T>) getJpaTemplate().execute(new JpaCallback() { public Object doInJpa(EntityManager entityManager) { Query query = entityManager.createQuery("Select e from " + getPersistentClass().getSimpleName() + " e "); return query.getResultList(); } }); }