List of usage examples for org.hibernate Query setParameter
@SuppressWarnings("unchecked") Query<R> setParameter(int position, Object val);
From source file:au.edu.anu.metadatastores.store.publication.PublicationService.java
License:Open Source License
/** * Get the publications by year// w w w . j a v a2 s. c o m * * @param year The year of publication to get * @return The a list of publications that were published in the given year */ public List<Publication> getPublicationsByYear(String year) { Session session = StoreHibernateUtil.getSessionFactory().openSession(); try { session.enableFilter("attributes"); Date startDate = new Date(); Query query = session.createQuery( "SELECT DISTINCT pub FROM PublicationItem pub inner join pub.itemAttributes pubYear join fetch pub.itemAttributes attrs left join fetch attrs.itemAttributes WHERE pubYear.attrType = :yearType and pubYear.attrValue = :yearValue"); query.setParameter("yearType", StoreAttributes.YEAR); query.setParameter("yearValue", year); @SuppressWarnings("unchecked") List<PublicationItem> items = query.list(); Date endDate = new Date(); long difference = endDate.getTime() - startDate.getTime(); LOGGER.debug("Time For Query: {}, Number of Records: {}", difference, items.size()); List<Publication> publications = new ArrayList<Publication>(); Publication publication = null; for (PublicationItem item : items) { publication = getPublication(item, true); publications.add(publication); } LOGGER.debug("Number of Publications: {}", items.size()); return publications; } finally { session.close(); } }
From source file:au.edu.anu.metadatastores.store.publication.PublicationService.java
License:Open Source License
/** * Get the publications associated with the user id * /*from w w w.j a v a 2 s. co m*/ * @param uid The user id to get publications for * @return A list of publications */ public List<Publication> getPersonsPublications(String uid) { Session session = StoreHibernateUtil.getSessionFactory().openSession(); try { session.enableFilter("attributes"); session.beginTransaction(); Query query = session.createQuery( "SELECT pub FROM PersonItem person inner join person.itemRelationsForRelatedIid pirfri inner join pirfri.itemByIid pubItem, PublicationItem pub WHERE person.extId = :extId and pubItem = pub"); query.setParameter("extId", uid); @SuppressWarnings("unchecked") List<PublicationItem> publicationItems = query.list(); List<Publication> publications = new ArrayList<Publication>(); for (PublicationItem publicationItem : publicationItems) { Publication publication = getPublication(publicationItem); publications.add(publication); } LOGGER.debug("Number of publications: {}", publications.size()); session.getTransaction().commit(); return publications; } finally { session.close(); } }
From source file:au.edu.anu.metadatastores.store.search.DBSearch.java
License:Open Source License
/** * Execute the query //from w w w. j a va 2s. co m * * @param session The hibernate session * @param queryString The query string * @param queryValue The value to search for * @return */ private List<ItemDTO> executeQueryWithParameters(Session session, String queryString, String queryValue) { Query query = session.createQuery(queryString); query.setParameter("attrValue", "%" + queryValue.toLowerCase() + "%"); @SuppressWarnings("unchecked") List<ItemDTO> items = query.list(); return items; }
From source file:au.edu.anu.metadatastores.store.search.DBSearch.java
License:Open Source License
/** * Execute the query with the given parameters * // w w w . j av a 2 s . c o m * @param session The hibernate session * @param queryString The query string * @param parameters The parameters * @return The list of items resulting from the query */ private List<ItemDTO> executeQueryWithParameters(Session session, String queryString, List<String> parameters) { Query query = session.createQuery(queryString); for (int i = 0; i < parameters.size(); i++) { query.setParameter(i, parameters.get(i)); } @SuppressWarnings("unchecked") List<ItemDTO> items = query.list(); return items; }
From source file:au.edu.anu.metadatastores.store.search.SearchService.java
License:Open Source License
/** * Execute the relationship query//from ww w . j a v a 2 s . c om * * @param session The hibernate session * @param queryString The query string * @param id The id of the object * @param system The system to filter the relation on if it exists * @return The related items */ private List<ItemDTO> executeRelationQuery(Session session, String queryString, Long id, String system) { Query query = session.createQuery(queryString.toString()); query.setParameter("id", id); if (system != null && system.length() > 0) { query.setParameter("system", system); } @SuppressWarnings("unchecked") List<ItemDTO> items = query.list(); return items; }
From source file:au.org.theark.core.dao.ArkAuthorisationDao.java
License:Open Source License
/** * List of ArkRolePolicyTemplates for users other than the super Admin. * //from w w w .j a v a 2 s . c o m * Here I was failed to use the projection list because I need the whole "ArkRolePolicyTemplate" object list * So I used instead of a query for that. * * Group by function. */ @SuppressWarnings("unchecked") public List<ArkRolePolicyTemplate> getArkRolePolicytemplateList(ArkUserRole arkUserRole) { String queryString = "SELECT arpt FROM ArkRolePolicyTemplate arpt where arpt.arkRole=(:arkRole) and " + "arpt.arkModule=(:arkModule) group by arpt.arkFunction, arpt.id"; Query query = getSession().createQuery(queryString); query.setParameter("arkRole", arkUserRole.getArkRole()); query.setParameter("arkModule", arkUserRole.getArkModule()); List<ArkRolePolicyTemplate> arkRolePolicyTemplateLst = query.list(); return arkRolePolicyTemplateLst; }
From source file:au.org.theark.core.dao.StudyDao.java
License:Open Source License
public boolean customFieldHasData(CustomField customField) { StringBuffer sb = new StringBuffer(); sb.append("SELECT count(*) FROM "); sb.append(" CustomField cf, "); sb.append(" CustomFieldDisplay cfd, "); sb.append(" PhenoData pd "); sb.append("WHERE cf.study.id = :studyId "); sb.append(" AND cf.arkFunction.id = :arkFunctionId "); sb.append(" AND cf.id = cfd.customField.id "); sb.append(" AND pd.customFieldDisplay.id = cfd.id"); Query query = getSession().createQuery(sb.toString()); query.setParameter("studyId", customField.getStudy().getId()); query.setParameter("arkFunctionId", customField.getArkFunction().getId()); return ((Number) query.iterate().next()).intValue() > 0; }
From source file:au.org.theark.core.dao.StudyDao.java
License:Open Source License
public long countNumberOfSubjectsThatAlreadyExistWithTheseUIDs(Study study, Collection<String> subjectUids) { String queryString = "select count(*) " + "from LinkSubjectStudy subject " + "where study =:study " + "and subjectUID in (:subjects) "; Query query = getSession().createQuery(queryString); query.setParameter("study", study); query.setParameterList("subjects", subjectUids); return (Long) query.uniqueResult(); }
From source file:au.org.theark.core.dao.StudyDao.java
License:Open Source License
public List<String> getSubjectUIDsThatAlreadyExistWithTheseUIDs(Study study, Collection<String> subjectUids) { String queryString = "select subject.subjectUID " + "from LinkSubjectStudy subject " + "where study =:study " + "and subjectUID in (:subjects) "; Query query = getSession().createQuery(queryString); query.setParameter("study", study); query.setParameterList("subjects", subjectUids); return query.list(); }
From source file:au.org.theark.core.dao.StudyDao.java
License:Open Source License
/** * based on sql concept of;4 select id from custom_field_display where custom_field_id in (SELECT id FROM custom_field where name='AGE' and * study_id = 1 and ark_function_id = 5) * //ww w .jav a 2 s . c o m * @param fieldNameCollection * @param study * @return */ @SuppressWarnings("unchecked") public List<CustomFieldDisplay> getCustomFieldDisplaysIn(List<String> fieldNameCollection, Study study, ArkFunction arkFunction) { if (fieldNameCollection == null || fieldNameCollection.isEmpty()) { return new ArrayList<CustomFieldDisplay>(); } else { List<String> lowerCaseNames = new ArrayList<String>(); for (String name : fieldNameCollection) { lowerCaseNames.add(name.toLowerCase()); } String queryString = "select cfd " + "from CustomFieldDisplay cfd " + "where customField.id in ( " + " SELECT id from CustomField cf " + " where cf.study =:study " + " and lower(cf.name) in (:names) " + " and cf.arkFunction =:arkFunction )"; Query query = getSession().createQuery(queryString); query.setParameter("study", study); // query.setParameterList("names", fieldNameCollection); query.setParameterList("names", lowerCaseNames); query.setParameter("arkFunction", arkFunction); return query.list(); } }