Example usage for org.hibernate Query list

List of usage examples for org.hibernate Query list

Introduction

In this page you can find the example usage for org.hibernate Query list.

Prototype

List<R> list();

Source Link

Document

Return the query results as a List.

Usage

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
 * //from  w  w w .  j av a  2s. co m
 * @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 people by name//from  ww w .j  a  va2  s. c o  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
 * //from   www  . j  a va2  s. c  o 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

/**
 * Retrieves a list of people with the given ids
 * //from w ww  .  j  av a 2  s  . c  o  m
 * @param extIds The ids to retrieve information about people for
 * @param extraInfo Indicates whether to also retrieve the type, institution, country and organisational unit about the person
 * @return A list of people containing basic information
 */
public List<Person> getBasicPeople(List<String> extIds, boolean extraInfo) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        Query query = session.createQuery(
                "select distinct pi from PersonItem as pi left join fetch pi.itemAttributes where pi.extId in (:extIds)");
        query.setParameterList("extIds", extIds);

        @SuppressWarnings("unchecked")
        List<PersonItem> personItems = query.list();
        LOGGER.debug("Number of People Found: {}", personItems.size());
        List<Person> people = new ArrayList<Person>();
        for (PersonItem personItem : personItems) {
            Person person = getBasicPerson(personItem, extraInfo);
            if (!people.contains(person)) {
                people.add(person);
            }
        }

        return people;
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.store.people.PersonService.java

License:Open Source License

public List<Person> getCurrentAriesPeople() {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {//from   ww  w  . j  a va2s .c  o  m
        //TODO limit this by current people?
        Query query = session
                .createQuery("FROM PersonItem item WHERE EXISTS (SELECT 1 FROM item.itemAttributes ia "
                        + "WHERE ia.attrType = :ariesAttrType) "
                        + "AND EXISTS (SELECT 1 FROM item.itemAttributes ia "
                        + "WHERE ia.attrType = :uidAttrType) "
                        + "AND EXISTS (SELECT 1 FROM item.itemAttributes ia "
                        + "WHERE ia.attrType = :activeAttr " + "AND ia.attrValue = :activeValue) ");
        query.setParameter("ariesAttrType", StoreAttributes.ARIES_ID);
        query.setParameter("uidAttrType", StoreAttributes.UNIVERSITY_ID);
        query.setParameter("activeAttr", StoreAttributes.ACTIVE);
        query.setParameter("activeValue", Boolean.TRUE.toString());
        @SuppressWarnings("unchecked")
        List<PersonItem> peopleItems = query.list();
        LOGGER.info("Number of people: {}", peopleItems.size());
        List<Person> people = getPeople(peopleItems.subList(0, Math.min(peopleItems.size(), 100)), true);
        //List<Person> people = getPeople(peopleItems, true);
        return people;
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.store.publication.PublicationService.java

License:Open Source License

/**
 * Searches for the people that match the publication with an author that has the provided given and surnames
 * //  w  ww.jav  a 2s .  c  o m
 * @param iid The item id of the publication
 * @param givenName The given name
 * @param surname The surname
 * @return The list of people that match
 */
public List<PersonItem> searchPublicationPerson(Long iid, String givenName, String surname) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        session.enableFilter("attributes");

        Query query = session.createQuery(
                "SELECT pi FROM ItemRelation ir join ir.itemByRelatedIid pi join pi.givenNames gn join pi.surnames sn WHERE ir.id.iid = :iid AND pi.extSystem = :extSystem AND lower(gn.attrValue) like :givenName and lower(sn.attrValue) = :surname");
        query.setParameter("iid", iid);
        query.setParameter("extSystem", "PERSON");
        query.setParameter("givenName", givenName.toLowerCase() + "%");
        query.setParameter("surname", surname.toLowerCase());

        @SuppressWarnings("unchecked")
        List<PersonItem> people = query.list();
        return people;
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.store.publication.PublicationService.java

License:Open Source License

/**
 * Get the publications by year/*from   www .  ja v  a 2 s  . c om*/
 * 
 * @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  av  a  2 s . c o 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   www  .java 2  s . 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
 * //from   w  w  w  .ja v a2s . com
 * @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;
}