Example usage for org.hibernate.query NativeQuery setParameter

List of usage examples for org.hibernate.query NativeQuery setParameter

Introduction

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

Prototype

@Override
    NativeQuery<T> setParameter(int position, Object value);

Source Link

Usage

From source file:org.openvpms.component.business.dao.hibernate.im.lookup.LookupReplacer.java

License:Open Source License

/**
 * Determines if a lookup is used by a particular archetype 'details' node.
 * <p/>/*from  ww  w .j  a  v a2  s .c o m*/
 * This uses SQL rather than HQL as HQL cannot query the details table.
 *
 * @param lookup    the lookup to check
 * @param node      the node
 * @param archetype the archetype
 * @param session   the hibernate session
 * @return <tt>true</tt> if the lookup is used, otherwise <tt>false</tt>
 */
private boolean isUsedSQL(Lookup lookup, NodeDescriptor node, ArchetypeDescriptor archetype, Session session) {
    Mapping mapping = getMapping(archetype, node);
    NativeQuery query = session.createSQLQuery(mapping.getIsUsedSQL());
    query.setMaxResults(1);
    query.setParameter("archetype", archetype.getType().getShortName());
    query.setParameter("name", node.getName());
    query.setParameter("code", lookup.getCode());
    return !query.list().isEmpty();
}

From source file:org.openvpms.component.business.dao.hibernate.im.lookup.LookupReplacer.java

License:Open Source License

/**
 * Replaces instances of a lookup where it is referred to by a 'details' node via its code.
 * <p/>//from w  w w  .j  a v a2s . c o m
 * This must be done using SQL, as HQL doesn't support updates to maps.
 *
 * @param node      the node descriptor that refers to the lookup's archetype
 * @param archetype the node's archetype descriptor
 * @param source    the lookup to replace
 * @param target    the lookup to replace <tt>source</tt> with
 * @param session   the Hibernate session
 */
private void replaceCodeSQL(NodeDescriptor node, ArchetypeDescriptor archetype, Lookup source, Lookup target,
        Session session) {
    Mapping mapping = getMapping(archetype, node);
    NativeQuery query = session.createSQLQuery(mapping.getUpdateSQL());
    query.setParameter("archetype", archetype.getType().getShortName());
    query.setParameter("name", node.getName());
    query.setParameter("oldCode", source.getCode());
    query.setParameter("newCode", target.getCode());
    executeUpdate(query, session, mapping.getPersistentClass());
}