List of usage examples for org.hibernate Session createQuery
@Override org.hibernate.query.Query createQuery(CriteriaDelete deleteQuery);
From source file:au.edu.anu.metadatastores.store.grants.GrantService.java
License:Open Source License
/** * Retrieves a list of grants associated with the person with the given id * /*from w w w.j a v a 2 s.c o m*/ * @param staffId The staff id of the person to retrieve grants for * @return The list of grants */ public List<Grant> getGrantsForPerson(String staffId) { Session session = StoreHibernateUtil.getSessionFactory().openSession(); try { session.enableFilter("attributes"); Query query = session.createQuery( "SELECT grant FROM GrantItem grant, PersonItem person join person.itemRelationsForRelatedIid personRelation WHERE personRelation.itemByIid = grant and person.extId = :staffId"); query.setParameter("staffId", staffId); @SuppressWarnings("unchecked") List<GrantItem> grantItems = query.list(); List<Grant> grants = new ArrayList<Grant>(); Grant grant = null; for (GrantItem grantItem : grantItems) { grant = getGrant(grantItem); if (grant != null) { grants.add(grant); } } return grants; } finally { session.close(); } }
From source file:au.edu.anu.metadatastores.store.grants.GrantService.java
License:Open Source License
/** * Find grants with the given attributes and attribute values * //from w ww . j av a 2 s . co m * @param attributes The attributes to query on * @return The grants */ public List<Grant> queryGrantsByAttributes(Map<String, String> attributes) { List<Grant> grants = new ArrayList<Grant>(); Session session = StoreHibernateUtil.getSessionFactory().openSession(); session.enableFilter("attributes"); try { List<String> parameters = new ArrayList<String>(); StringBuilder fromString = new StringBuilder(); StringBuilder whereString = new StringBuilder(); fromString.append(" FROM GrantItem gi"); whereString.append(" WHERE"); int i = 0; for (Entry<String, String> entry : attributes.entrySet()) { fromString.append(" LEFT JOIN gi.itemAttributes gia"); fromString.append(i); if (i > 0) { whereString.append(" AND"); } whereString.append(" gia"); whereString.append(i); whereString.append(".attrType = ? AND lower(gia"); whereString.append(i); whereString.append(".attrValue) like ?"); parameters.add(entry.getKey()); parameters.add("%" + entry.getValue().toLowerCase() + "%"); i++; } String queryString = "SELECT gi " + fromString.toString() + " " + whereString.toString(); LOGGER.info("Query: {}", queryString); LOGGER.info("Number of parameters: {}", parameters.size()); Query query = session.createQuery(queryString); for (i = 0; i < parameters.size(); i++) { query.setParameter(i, parameters.get(i)); } @SuppressWarnings("unchecked") List<GrantItem> grantItems = query.list(); Grant grant = null; for (GrantItem grantItem : grantItems) { grant = getGrant(grantItem); grants.add(grant); } } finally { session.close(); } return grants; }
From source file:au.edu.anu.metadatastores.store.misc.Mappings.java
License:Open Source License
public static RelationMapping getMappingByDescription(String description) { Session session = StoreHibernateUtil.getSessionFactory().openSession(); Query query = session.createQuery("FROM RelationMapping WHERE description = :description"); query.setParameter("description", description); RelationMapping mapping = (RelationMapping) query.uniqueResult(); session.close();/*from w w w. j a v a2s . c om*/ return mapping; }
From source file:au.edu.anu.metadatastores.store.misc.RelationService.java
License:Open Source License
/** * Get the potential relations/* ww w . j a v a 2 s . c om*/ * * @return The potential relations */ public List<Relation> getPotentialRelations() { Session session = StoreHibernateUtil.getSessionFactory().openSession(); try { Query query = session.createQuery( "SELECT new au.edu.anu.metadatastores.store.misc.Relation(pr.itemByIid.iid, pr.itemByIid.title, pr.id.relationValue, pr.itemByRelatedIid.iid, pr.itemByRelatedIid.title) FROM PotentialRelation pr WHERE pr.requireCheck = true"); @SuppressWarnings("unchecked") List<Relation> relations = query.list(); return relations; } finally { session.close(); } }
From source file:au.edu.anu.metadatastores.store.misc.RelationService.java
License:Open Source License
/** * Get the relations for the item with the given id * /*w ww . j a va 2 s. c om*/ * @param iid The item id * @return The list of relations */ public List<Relation> getRelatedItems(Long iid) { Session session = StoreHibernateUtil.getSessionFactory().openSession(); try { // Get the direct relations Query query = session.createQuery( "select new au.edu.anu.metadatastores.store.misc.Relation(i.iid, i.title, rm.description, ir.itemByRelatedIid.iid, ir.itemByRelatedIid.title) from Item i join i.itemRelationsForIid ir, RelationMapping rm where i.iid = :id and ir.id.relationValue = rm.code"); query.setParameter("id", iid); @SuppressWarnings("unchecked") List<Relation> results = query.list(); Set<Relation> relations = new HashSet<Relation>(); relations.addAll(results); // Get the reverse relations Query reverseQuery = session.createQuery( "select new au.edu.anu.metadatastores.store.misc.Relation(i.iid, i.title, rrm.description, ir.itemByIid.iid, ir.itemByIid.title) from Item i join i.itemRelationsForRelatedIid ir, RelationMapping rm, RelationMapping rrm where i.iid = :id and ir.id.relationValue = rm.code and rm.reverse = rrm.code"); reverseQuery.setParameter("id", iid); @SuppressWarnings("unchecked") List<Relation> results2 = reverseQuery.list(); relations.addAll(results2); LOGGER.debug("Number of relations: {}", relations.size()); return new ArrayList<Relation>(relations); } finally { session.close(); } }
From source file:au.edu.anu.metadatastores.store.people.PersonService.java
License:Open Source License
/** * Find a person by a uid//from ww w.ja va 2 s.c o m * * @param uid The uid of the person to find * @return Get the Item of the person with the given uid */ private PersonItem queryPersonByUid(String uid) { LOGGER.info("uid: {}", uid); Session session = StoreHibernateUtil.getSessionFactory().openSession(); try { session.enableFilter("attributes"); Query query = session.createQuery("from PersonItem where lower(extId) = :extId"); query.setParameter("extId", uid.toLowerCase()); PersonItem personItem = (PersonItem) query.uniqueResult(); return personItem; } finally { session.close(); } }
From source file:au.edu.anu.metadatastores.store.people.PersonService.java
License:Open Source License
/** * Find people by name//from w w w. j a v a 2 s . co m * * @param givenName The given name to search on * @param surname The surname to search on * @return The list of people with the name */ private List<PersonItem> queryPeopleByName(String givenName, String surname) { LOGGER.debug("Given Name: {}, Surname: {}", givenName, surname); Session session = StoreHibernateUtil.getSessionFactory().openSession(); try { session.enableFilter("attributes"); Query query = session.createQuery( "select pi from PersonItem pi join pi.givenNames gn join pi.surnames sn where lower(gn.attrValue) = :givenName and lower(sn.attrValue) = :surname"); query.setParameter("givenName", givenName.toLowerCase()); query.setParameter("surname", surname.toLowerCase()); @SuppressWarnings("unchecked") List<PersonItem> people = query.list(); if (people != null) { LOGGER.debug("Number of people found in first query: {}", people.size()); } else { LOGGER.debug("No people found in first query"); } if (people == null || people.size() == 0) { query = session.createQuery( "select pi from PersonItem pi join pi.commonNames cn where lower(cn.attrValue) = :commonName"); query.setParameter("commonName", givenName.toLowerCase() + " " + surname.toLowerCase()); people = query.list(); if (people != null) { LOGGER.debug("Number of people found in second query: {}", people.size()); } else { LOGGER.debug("No people found in second query"); } } return people; } finally { session.close(); } }
From source file:au.edu.anu.metadatastores.store.people.PersonService.java
License:Open Source License
/** * Find people with the given attributes and attribute values * //w w w . j a va2s . co m * @param attributes The attribute/value pairs to search on * @return A list of people with the given attributes */ public List<Person> queryPersonByAttributes(Map<String, String> attributes) { List<Person> people = new ArrayList<Person>(); Session session = StoreHibernateUtil.getSessionFactory().openSession(); try { List<String> parameters = new ArrayList<String>(); StringBuilder fromString = new StringBuilder(); StringBuilder whereString = new StringBuilder(); fromString.append(" FROM PersonItem pi"); whereString.append(" WHERE"); int i = 0; for (Entry<String, String> entry : attributes.entrySet()) { fromString.append(" LEFT JOIN pi.itemAttributes pia"); fromString.append(i); if (i > 0) { whereString.append(" AND"); } whereString.append(" pia"); whereString.append(i); whereString.append(".attrType = ? AND lower(pia"); whereString.append(i); whereString.append(".attrValue) like ?"); parameters.add(entry.getKey()); parameters.add(entry.getValue().toLowerCase() + "%"); i++; } String queryString = "SELECT pi " + fromString.toString() + " " + whereString.toString(); LOGGER.debug("Query: {}", queryString); LOGGER.debug("Number of parameters: {}", parameters.size()); Query query = session.createQuery(queryString); for (i = 0; i < parameters.size(); i++) { query.setParameter(i, parameters.get(i)); } @SuppressWarnings("unchecked") List<PersonItem> personItems = query.list(); Person person = null; for (PersonItem personItem : personItems) { person = getPerson(personItem, false); people.add(person); } } finally { session.close(); } return people; }
From source file:au.edu.anu.metadatastores.store.people.PersonService.java
License:Open Source License
/** * Save the person's information// w w w.j a va 2 s. c o m * * @param person The person to save * @param userUpdated Indicates whether the information is user updated or not * @return The item information for the person */ public PersonItem savePerson(Person person, Boolean userUpdated) { if (person.getExtId() == null) { return null; } Session session = StoreHibernateUtil.getSessionFactory().openSession(); try { session.beginTransaction(); session.enableFilter("attributes"); Query query = session.createQuery("from PersonItem where extId = :extId"); query.setParameter("extId", person.getExtId()); PersonItem item = (PersonItem) query.uniqueResult(); String title = person.getFullName(); Date lastModified = new Date(); ItemTraitParser parser = new ItemTraitParser(); Item newItem = null; try { newItem = parser.getItem(person, userUpdated, lastModified); } catch (Exception e) { LOGGER.error("Exception transforming person to an item", e); } if (item == null) { item = new PersonItem(); item.setExtId(person.getExtId().toLowerCase()); item.setTitle(title); item = (PersonItem) session.merge(item); } else { item.setTitle(title); } updateAttributesFromItem(item, newItem, session, lastModified); item = (PersonItem) session.merge(item); session.getTransaction().commit(); return item; } finally { session.close(); } }
From source file:au.edu.anu.metadatastores.store.people.PersonService.java
License:Open Source License
/** * Retrieve basic information about a person e.g. their name * /*from w ww . j a v a2 s . com*/ * @param uid The unique id of the person to retrieve information about * @return The information about the person */ public Person getPerson(String uid) { Session session = StoreHibernateUtil.getSessionFactory().openSession(); try { session.enableFilter("attributes"); Transaction transaction = session.beginTransaction(); Query query = session.createQuery("from PersonItem where extId = :extId"); query.setParameter("extId", uid); PersonItem item = (PersonItem) query.uniqueResult(); Person person = null; if (item != null) { person = getPerson(item); } transaction.commit(); return person; } finally { session.close(); } }