List of usage examples for javax.persistence TypedQuery setParameter
TypedQuery<X> setParameter(int position, Object value);
From source file:models.Service.java
public static Page<Service> queryServiceByPage(int page, int pageSize, User user) { if (user == null) { throw new IllegalArgumentException("user?"); }// ww w.j a va2s .c om StringBuffer queryHql = new StringBuffer(); StringBuffer countHql = new StringBuffer(); Map<String, Object> paramMap = new HashMap<String, Object>(); queryHql.append("from Service s where s.owner.id = :userId"); countHql.append("select count(s.id) from Service s where s.owner.id = :userId"); paramMap.put("userId", user.getId()); queryHql.append(" order by s.createDate desc"); TypedQuery<Service> listQuery = JPA.em().createQuery(queryHql.toString(), Service.class); TypedQuery<Long> countQuery = JPA.em().createQuery(countHql.toString(), Long.class); for (Entry<String, Object> e : paramMap.entrySet()) { listQuery.setParameter(e.getKey(), e.getValue()); countQuery.setParameter(e.getKey(), e.getValue()); } List<Service> data = listQuery.setFirstResult(page * pageSize).setMaxResults(pageSize).getResultList(); Long count = countQuery.getSingleResult(); return new Page<Service>(Constants.SUCESS, count, data); }
From source file:net.triptech.metahive.model.Definition.java
/** * Find the top level definitions (i.e. those that are not summarised). * * @param applicability the applicability of the definition to filter by * @return the list of definitions/*from w w w . j av a 2 s .c o m*/ */ public static List<Definition> findTopLevelDefinitions(final Applicability applicability) { StringBuilder sql = new StringBuilder("SELECT d FROM Definition d"); sql.append(" WHERE d.summaryDefinition IS NULL"); if (applicability != null) { sql.append(" AND d.applicability = :applicability"); } sql.append(" ORDER BY d.name ASC"); TypedQuery<Definition> q = entityManager().createQuery(sql.toString(), Definition.class); if (applicability != null) { q.setParameter("applicability", applicability); } return q.getResultList(); }
From source file:models.Service.java
public static Page<Service> queryServiceByPage(int page, int pageSize, Long userId, String searchText, Long industryId, String skillTag, boolean isFetchUser, boolean isFetchUserExpert) { StringBuffer queryHql = new StringBuffer(); StringBuffer countHql = new StringBuffer(); Map<String, Object> paramMap = new HashMap<String, Object>(); queryHql.append("from Service s "); countHql.append("select count(s.id) from Service s where 1=1 "); if (isFetchUser) { queryHql.append(" left join fetch s.owner o "); }//www .j av a 2s . c o m if (isFetchUser && isFetchUserExpert) { queryHql.append(" left join fetch o.experts "); } queryHql.append(" where 1=1 "); if (null != userId) { queryHql.append(" and s.owner.id = :userId "); countHql.append(" and s.owner.id = :userId "); paramMap.put("userId", userId); } if (StringUtils.isNotBlank(searchText)) { queryHql.append( " and (s.industry.tagName like :searchTextLike or s.title like :searchTextLike or s.price like :searchTextLike)"); countHql.append( " and (s.industry.tagName like :searchTextLike or s.title like :searchTextLike or s.price like :searchTextLike)"); paramMap.put("searchTextLike", "%" + searchText.trim() + "%"); } if (null != industryId) { queryHql.append(" and s.industry.id = :industryId "); countHql.append(" and s.industry.id = :industryId "); paramMap.put("industryId", industryId); } if (StringUtils.isNotBlank(skillTag)) { queryHql.append(" and s.tags like :skillTag "); countHql.append(" and s.tags like :skillTag "); paramMap.put("skillTag", "%" + skillTag + "%"); } queryHql.append(" order by s.createDate desc"); TypedQuery<Service> listQuery = JPA.em().createQuery(queryHql.toString(), Service.class); TypedQuery<Long> countQuery = JPA.em().createQuery(countHql.toString(), Long.class); for (Entry<String, Object> e : paramMap.entrySet()) { listQuery.setParameter(e.getKey(), e.getValue()); countQuery.setParameter(e.getKey(), e.getValue()); } List<Service> data = listQuery.setFirstResult(page * pageSize).setMaxResults(pageSize).getResultList(); Long count = countQuery.getSingleResult(); return new Page<Service>(Constants.SUCESS, count, data); }
From source file:net.triptech.metahive.model.Definition.java
/** * A helper function to find summarised definitions * based on the supplied definition id.//from ww w. j ava 2s .com * * @param def the definition * @return the list */ public static List<Definition> findSummarisedDefinitions(final Definition def) { TypedQuery<Definition> q = entityManager() .createQuery("SELECT d FROM Definition AS d WHERE d.summaryDefinition = :def", Definition.class); q.setParameter("def", def); return q.getResultList(); }
From source file:net.triptech.metahive.model.Definition.java
/** * Find the definition which is the unique identifier. * * @param name/*from w w w . j a v a2 s . co m*/ * the name * @return the definition */ public static Definition findUniqueIdentifierDefinition() { Definition definition = null; TypedQuery<Definition> q = entityManager() .createQuery("SELECT d FROM Definition AS d WHERE d.dataType = :dataType", Definition.class); q.setParameter("dataType", DataType.TYPE_UNIQUEID); List<Definition> definitions = q.getResultList(); if (definitions != null && definitions.size() > 0) { definition = definitions.get(0); } return definition; }
From source file:net.triptech.metahive.model.Definition.java
/** * Find the definition going by the supplied name. * * @param name/*from w ww . j ava 2 s .c o m*/ * the name * @return the definition */ public static Definition findDefinitionByNameEquals(String name) { Definition definition = null; if (name == null || name.length() == 0) { throw new IllegalArgumentException("The name argument is required"); } TypedQuery<Definition> q = entityManager() .createQuery("SELECT d FROM Definition AS d WHERE LOWER(d.name) = LOWER(:name)", Definition.class); q.setParameter("name", name); List<Definition> definitions = q.getResultList(); if (definitions != null && definitions.size() > 0) { definition = definitions.get(0); } return definition; }
From source file:net.triptech.metahive.model.Definition.java
/** * Count the definitions./*from w ww . j av a 2 s . c o m*/ * * @param filter the filter * @return the long */ public static long countDefinitions(final DefinitionFilter filter) { StringBuilder sql = new StringBuilder("SELECT COUNT(d) FROM Definition d JOIN d.category c"); sql.append(buildWhere(filter)); TypedQuery<Long> q = entityManager().createQuery(sql.toString(), Long.class); HashMap<String, String> variables = buildVariables(filter); for (String variable : variables.keySet()) { q.setParameter(variable, variables.get(variable)); } return q.getSingleResult(); }
From source file:net.triptech.metahive.model.Definition.java
/** * Find the definitions that could be potentially related to the supplied definition. * * @param definition the definition//from ww w. ja v a 2 s.c o m * @return the list */ public static List<Definition> findPotentialRelatedDefinitions(final Definition definition) { List<Definition> relatedDefinitions = new ArrayList<Definition>(); HashMap<String, Object> variables = new HashMap<String, Object>(); StringBuilder sql = new StringBuilder(); if (definition != null) { StringBuilder where = new StringBuilder(); List<Definition> defs = new ArrayList<Definition>(); if (definition.getDefinitionType() == DefinitionType.CALCULATED) { defs = definition.getCalculatedDefinitions(); } if (definition.getDefinitionType() == DefinitionType.SUMMARY) { defs = definition.getSummarisedDefinitions(); } for (Definition def : defs) { if (where.length() > 0) { where.append(" AND "); } where.append("d.id != "); where.append(def.getId()); } if (where.length() > 0) { where.insert(0, "("); where.append(")"); } if (definition.getDefinitionType() == DefinitionType.CALCULATED) { if (where.length() > 0) { where.append(" AND "); } where.append("(d.dataType = :dataType1 OR d.dataType = :dataType2"); where.append(" OR d.dataType = :dataType3)"); variables.put("dataType1", DataType.TYPE_CURRENCY); variables.put("dataType2", DataType.TYPE_NUMBER); variables.put("dataType3", DataType.TYPE_PERCENTAGE); } if (definition.getDefinitionType() == DefinitionType.SUMMARY) { if (where.length() > 0) { where.append(" AND "); } where.append("d.definitionType != :definitionType"); where.append(" AND d.summaryDefinition is null"); variables.put("definitionType", DefinitionType.SUMMARY); } if (definition.getDefinitionType() == DefinitionType.CALCULATED || definition.getDefinitionType() == DefinitionType.SUMMARY) { // Load all of the definitions, less the definitions already associated sql.append("SELECT d FROM Definition d"); if (where.length() > 0) { sql.append(" WHERE "); sql.append(where.toString()); } sql.append(" ORDER BY d.name ASC"); } } if (sql.length() > 0) { TypedQuery<Definition> q = entityManager().createQuery(sql.toString(), Definition.class); for (String variable : variables.keySet()) { q.setParameter(variable, variables.get(variable)); } relatedDefinitions = q.getResultList(); } return relatedDefinitions; }
From source file:com.clustercontrol.jobmanagement.util.QueryUtil.java
public static List<JobKickEntity> getJobKickEntityFindByFilter(String jobkickId, String jobkickName, Integer jobkickType, String jobunitId, String jobId, String calendarId, Boolean validFlg, String regUser, Long regFromDate, Long regToDate, String updateUser, Long updateFromDate, Long updateToDate, String ownerRoleId) { HinemosEntityManager em = new JpaTransactionManager().getEntityManager(); // ?????????? String notInclude = "NOT:"; // ????/*w w w .ja v a2 s .c o m*/ StringBuffer sbJpql = new StringBuffer(); sbJpql.append("SELECT a FROM JobKickEntity a WHERE true = true"); // jobkickId if (jobkickId != null && !"".equals(jobkickId)) { if (!jobkickId.startsWith(notInclude)) { sbJpql.append(" AND a.jobkickId like :jobkickId"); } else { sbJpql.append(" AND a.jobkickId not like :jobkickId"); } } // jobkickName if (jobkickName != null && !"".equals(jobkickName)) { if (!jobkickName.startsWith(notInclude)) { sbJpql.append(" AND a.jobkickName like :jobkickName"); } else { sbJpql.append(" AND a.jobkickName not like :jobkickName"); } } // jobkickType if (jobkickType != null) { sbJpql.append(" AND a.jobkickType = :jobkickType"); } // jobunitId if (jobunitId != null && !"".equals(jobunitId)) { if (!jobunitId.startsWith(notInclude)) { sbJpql.append(" AND a.jobunitId like :jobunitId"); } else { sbJpql.append(" AND a.jobunitId not like :jobunitId"); } } // jobId if (jobId != null && !"".equals(jobId)) { if (!jobId.startsWith(notInclude)) { sbJpql.append(" AND a.jobId like :jobId"); } else { sbJpql.append(" AND a.jobId not like :jobId"); } } // calendarId if (calendarId != null && !"".equals(calendarId)) { sbJpql.append(" AND a.calendarId like :calendarId"); } // validFlg if (validFlg != null) { sbJpql.append(" AND a.validFlg = :validFlg"); } // regUser if (regUser != null && !"".equals(regUser)) { if (!regUser.startsWith(notInclude)) { sbJpql.append(" AND a.regUser like :regUser"); } else { sbJpql.append(" AND a.regUser not like :regUser"); } } // regFromDate if (regFromDate > 0) { sbJpql.append(" AND a.regDate >= :regFromDate"); } // regToDate if (regToDate > 0) { sbJpql.append(" AND a.regDate <= :regToDate"); } // updateUser if (updateUser != null && !"".equals(updateUser)) { if (!updateUser.startsWith(notInclude)) { sbJpql.append(" AND a.updateUser like :updateUser"); } else { sbJpql.append(" AND a.updateUser not like :updateUser"); } } // updateFromDate if (updateFromDate > 0) { sbJpql.append(" AND a.updateDate >= :updateFromDate"); } // updateToDate if (updateToDate > 0) { sbJpql.append(" AND a.updateDate <= :updateToDate"); } // ownerRoleId if (ownerRoleId != null && !"".equals(ownerRoleId)) { if (!ownerRoleId.startsWith(notInclude)) { sbJpql.append(" AND a.ownerRoleId like :ownerRoleId"); } else { sbJpql.append(" AND a.ownerRoleId not like :ownerRoleId"); } } TypedQuery<JobKickEntity> typedQuery = em.createQuery(sbJpql.toString(), JobKickEntity.class); // jobkickId if (jobkickId != null && !"".equals(jobkickId)) { if (!jobkickId.startsWith(notInclude)) { typedQuery = typedQuery.setParameter("jobkickId", jobkickId); } else { typedQuery = typedQuery.setParameter("jobkickId", jobkickId.substring(notInclude.length())); } } // jobkickName if (jobkickName != null && !"".equals(jobkickName)) { if (!jobkickName.startsWith(notInclude)) { typedQuery = typedQuery.setParameter("jobkickName", jobkickName); } else { typedQuery = typedQuery.setParameter("jobkickName", jobkickName.substring(notInclude.length())); } } // jobkickType if (jobkickType != null) { typedQuery = typedQuery.setParameter("jobkickType", jobkickType); } // jobunitId if (jobunitId != null && !"".equals(jobunitId)) { if (!jobunitId.startsWith(notInclude)) { typedQuery = typedQuery.setParameter("jobunitId", jobunitId); } else { typedQuery = typedQuery.setParameter("jobunitId", jobunitId.substring(notInclude.length())); } } // jobId if (jobId != null && !"".equals(jobId)) { if (!jobId.startsWith(notInclude)) { typedQuery = typedQuery.setParameter("jobId", jobId); } else { typedQuery = typedQuery.setParameter("jobId", jobId.substring(notInclude.length())); } } // calendarId if (calendarId != null && !"".equals(calendarId)) { typedQuery = typedQuery.setParameter("calendarId", calendarId); } // validFlg if (validFlg != null) { typedQuery = typedQuery.setParameter("validFlg", validFlg); } // regUser if (regUser != null && !"".equals(regUser)) { if (!regUser.startsWith(notInclude)) { typedQuery = typedQuery.setParameter("regUser", regUser); } else { typedQuery = typedQuery.setParameter("regUser", regUser.substring(notInclude.length())); } } // regFromDate if (regFromDate > 0) { typedQuery = typedQuery.setParameter("regFromDate", regFromDate); } // regToDate if (regToDate > 0) { typedQuery = typedQuery.setParameter("regToDate", regToDate); } // updateUser if (updateUser != null && !"".equals(updateUser)) { if (!updateUser.startsWith(notInclude)) { typedQuery = typedQuery.setParameter("updateUser", updateUser); } else { typedQuery = typedQuery.setParameter("updateUser", updateUser.substring(notInclude.length())); } } // updateFromDate if (updateFromDate > 0) { typedQuery = typedQuery.setParameter("updateFromDate", updateFromDate); } // updateToDate if (updateToDate > 0) { typedQuery = typedQuery.setParameter("updateToDate", updateToDate); } // ownerRoleId if (ownerRoleId != null && !"".equals(ownerRoleId)) { if (!ownerRoleId.startsWith(notInclude)) { typedQuery = typedQuery.setParameter("ownerRoleId", ownerRoleId); } else { typedQuery = typedQuery.setParameter("ownerRoleId", ownerRoleId.substring(notInclude.length())); } } return typedQuery.getResultList(); }
From source file:net.triptech.metahive.model.Definition.java
/** * Find definition entries.//w w w . j a v a2 s . c o m * * @param filter the definition filter * @param firstResult the first result * @param maxResults the max results * @return the list */ public static List<Definition> findDefinitionEntries(final DefinitionFilter filter, final int firstResult, final int maxResults) { StringBuilder sql = new StringBuilder("SELECT d FROM Definition d JOIN d.category c"); sql.append(buildWhere(filter)); sql.append(" ORDER BY d.name ASC"); TypedQuery<Definition> q = entityManager().createQuery(sql.toString(), Definition.class) .setFirstResult(firstResult).setMaxResults(maxResults); HashMap<String, String> variables = buildVariables(filter); for (String variable : variables.keySet()) { q.setParameter(variable, variables.get(variable)); } return q.getResultList(); }