Example usage for org.hibernate Query setParameter

List of usage examples for org.hibernate Query setParameter

Introduction

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

Prototype

@SuppressWarnings("unchecked")
Query<R> setParameter(int position, Object val);

Source Link

Document

Bind a positional query parameter using its inferred Type.

Usage

From source file:au.edu.anu.metadatastores.store.digitalcollections.DigitalCollectionsService.java

License:Open Source License

public DigitalCollection getDigitalCollection(String id) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    session.enableFetchProfile("attributes");
    //DublinCore digitalCollection = null;
    try {//  ww w .j  ava  2 s.  c  o  m
        Query query = session.createQuery("FROM DigitalCollectionItem WHERE extId = :extId");
        query.setParameter("extId", id);

        DigitalCollectionsItem item = (DigitalCollectionsItem) query.uniqueResult();
        //digitalCollection = getDigitalCollection(item);
        return getDigitalCollection(item);
    } finally {
        session.close();
    }

    //return digitalCollection;
}

From source file:au.edu.anu.metadatastores.store.epress.EpressService.java

License:Open Source License

/**
 * Save a single E Press record/*from ww w  . j  av  a  2 s . c  om*/
 * 
 * @param epress The E Press record t osave
 * @return The E Press record item
 */
public EpressItem saveEpress(Epress epress, Boolean userUpdated) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();

    try {
        session.beginTransaction();

        Query query = session.createQuery("FROM EpressItem WHERE extId = :extId");
        query.setParameter("extId", epress.getExtId());
        EpressItem item = (EpressItem) query.uniqueResult();

        Date lastModified = new Date();

        ItemTraitParser parser = new ItemTraitParser();
        Item newItem = null;
        newItem = parser.getItem(epress, Boolean.FALSE, lastModified);

        if (item == null) {
            item = new EpressItem();
            if (newItem.getExtId() == null) {
                return null;
            }
            item.setExtId(newItem.getExtId());
            session.save(item);
        }

        updateAttributesFromItem(item, newItem, session, lastModified);

        session.getTransaction().commit();
        return item;
    } catch (IllegalAccessException e) {
        LOGGER.error("Exception accessing field when trying to get an e press item", e);
    } catch (InvocationTargetException e) {
        LOGGER.error("Exception invoking method when trying to get an e press item", e);
    } finally {
        session.close();
    }

    return null;
}

From source file:au.edu.anu.metadatastores.store.epress.EpressService.java

License:Open Source License

/**
 * Get the E Press record by its ext id (N.B. this is the ISBN or ISSN)
 * //from   w w w .j  a v a 2 s  .  c  om
 * @param epressId The external id
 * @return The E Press record that was found
 */
public Epress getEpress(String epressId) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    session.enableFilter("attributes");
    try {
        Query query = session.createQuery("FROM EpressItem WHERE extId = :extId");
        query.setParameter("extId", epressId);

        EpressItem item = (EpressItem) query.uniqueResult();
        Epress epress = getEpress(item);
        return epress;
    } finally {
        session.close();
    }
}

From source file:au.edu.anu.metadatastores.store.grants.GrantService.java

License:Open Source License

/**
 * Save the grant information/*  w w w .j a va 2  s .  co m*/
 * 
 * @param grant The grant to save
 * @param userUpdated Indicator of whether it is user updated or system updated
 * @return The GrantItem that has been created/updated
 */
public GrantItem saveGrant(Grant grant, Boolean userUpdated) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();

    GrantItem item = null;
    try {
        session.enableFilter("attributes");
        session.beginTransaction();

        Query query = session.createQuery("from GrantItem where extId = :extId");
        query.setParameter("extId", grant.getContractCode());

        item = (GrantItem) query.uniqueResult();

        Date lastModified = new Date();
        ItemTraitParser parser = new ItemTraitParser();
        Item newItem = null;
        newItem = parser.getItem(grant, userUpdated, lastModified);

        if (item == null) {
            item = new GrantItem();
            if (newItem.getExtId() == null) {
                return null;
            }
            item.setExtId(newItem.getExtId());
            session.save(item);
        }

        updateAttributesFromItem(item, newItem, session, lastModified);

        LOGGER.debug("Number of item attributes before: {}", item.getItemAttributes().size());

        associatePeople(item, grant, session);

        LOGGER.debug("Number of Item Attributes after: {}", item.getItemAttributes().size());

        item = (GrantItem) session.merge(item);
        session.getTransaction().commit();
    } catch (IllegalAccessException e) {
        LOGGER.error("Exception accessing field when trying to get a grant item", e);
    } catch (InvocationTargetException e) {
        LOGGER.error("Exception invoking method when trying to get a grant item", e);
    } catch (Exception e) {
        if (item == null) {
            LOGGER.error("Exception querying item", e);
        } else {
            LOGGER.error("Error Merging Item {}", item.getIid(), e);
            LOGGER.info("Error with item: {}, Title: {}, System: {}, Ext Id: {}", item.getIid(),
                    item.getTitle(), item.getExtSystem(), item.getExtId());
            for (ItemAttribute attr : item.getItemAttributes()) {
                LOGGER.info("AID: {}, IID: {}, Type: {}, Value: {}", new Object[] { attr.getAid(),
                        attr.getItem().getIid(), attr.getAttrType(), attr.getAttrValue() });
            }
            for (HistItemAttribute attr : item.getHistItemAttributes()) {
                LOGGER.info("AID: {}, Date: {}, Type: {}, Value: {}", new Object[] { attr.getId().getAid(),
                        attr.getId().getHistDatetime(), attr.getAttrType(), attr.getAttrValue() });
            }
        }
    } finally {
        session.close();
    }

    return item;
}

From source file:au.edu.anu.metadatastores.store.grants.GrantService.java

License:Open Source License

/**
 * Return the Grant with the given id// w ww . j av  a  2 s  . c  o  m
 * 
 * @param grantId The id of the grant to retrieve
 * @return The grant information
 */
public Grant getGrant(String grantId) {
    Session session = StoreHibernateUtil.getSessionFactory().openSession();
    try {
        session.enableFilter("attributes");

        Query query = session.createQuery("FROM GrantItem WHERE extId = :extId");
        query.setParameter("extId", grantId);

        GrantItem item = (GrantItem) query.uniqueResult();
        Grant grant = getGrant(item);
        return grant;
    } finally {
        session.close();
    }
}

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  va  2  s.  com
 * @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
 * //  w  w w .ja  v  a  2s  . c o  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();// w  ww  . j ava2  s  .  com

    return mapping;
}

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  . ja v a 2 s  .  com
 * @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//ww w  .  j ava 2s  .c om
 * 
 * @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();
    }
}