Example usage for org.hibernate Query setText

List of usage examples for org.hibernate Query setText

Introduction

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

Prototype

@Deprecated
@SuppressWarnings("unchecked")
default Query<R> setText(String name, String val) 

Source Link

Document

Bind a named String-valued parameter using streaming.

Usage

From source file:com.xpn.xwiki.store.XWikiHibernateStore.java

License:Open Source License

public XWikiDocument loadXWikiDoc(XWikiDocument doc, XWikiContext context) throws XWikiException {
    // To change body of implemented methods use Options | File Templates.
    boolean bTransaction = true;
    MonitorPlugin monitor = Util.getMonitorPlugin(context);
    try {// w ww  .ja v  a  2  s .com
        // Start monitoring timer
        if (monitor != null) {
            monitor.startTimer("hibernate");
        }
        doc.setStore(this);
        checkHibernate(context);

        SessionFactory sfactory = injectCustomMappingsInSessionFactory(doc, context);
        bTransaction = bTransaction && beginTransaction(sfactory, false, context);
        Session session = getSession(context);
        session.setFlushMode(FlushMode.MANUAL);

        try {
            session.load(doc, new Long(doc.getId()));
            doc.setDatabase(context.getDatabase());
            doc.setNew(false);
            doc.setMostRecent(true);
            // Fix for XWIKI-1651
            doc.setDate(new Date(doc.getDate().getTime()));
            doc.setCreationDate(new Date(doc.getCreationDate().getTime()));
            doc.setContentUpdateDate(new Date(doc.getContentUpdateDate().getTime()));
        } catch (ObjectNotFoundException e) { // No document
            doc.setNew(true);
            return doc;
        }

        // Loading the attachment list
        if (doc.hasElement(XWikiDocument.HAS_ATTACHMENTS)) {
            loadAttachmentList(doc, context, false);
        }

        // TODO: handle the case where there are no xWikiClass and xWikiObject in the Database
        BaseClass bclass = new BaseClass();
        String cxml = doc.getXClassXML();
        if (cxml != null) {
            bclass.fromXML(cxml);
            bclass.setDocumentReference(doc.getDocumentReference());
            doc.setXClass(bclass);
        }

        // Store this XWikiClass in the context so that we can use it in case of recursive usage
        // of classes
        context.addBaseClass(bclass);

        if (doc.hasElement(XWikiDocument.HAS_OBJECTS)) {
            Query query = session.createQuery(
                    "from BaseObject as bobject where bobject.name = :name order by " + "bobject.number");
            query.setText("name", doc.getFullName());
            @SuppressWarnings("unchecked")
            Iterator<BaseObject> it = query.list().iterator();

            EntityReference localGroupEntityReference = new EntityReference("XWikiGroups", EntityType.DOCUMENT,
                    new EntityReference("XWiki", EntityType.SPACE));
            DocumentReference groupsDocumentReference = new DocumentReference(context.getDatabase(),
                    localGroupEntityReference.getParent().getName(), localGroupEntityReference.getName());

            boolean hasGroups = false;
            while (it.hasNext()) {
                BaseObject object = it.next();
                DocumentReference classReference = object.getXClassReference();

                if (classReference == null) {
                    continue;
                }

                // It seems to search before is case insensitive. And this would break the loading if we get an
                // object which doesn't really belong to this document
                if (!object.getDocumentReference().equals(doc.getDocumentReference())) {
                    continue;
                }

                BaseObject newobject;
                if (classReference.equals(doc.getDocumentReference())) {
                    newobject = bclass.newCustomClassInstance(context);
                } else {
                    newobject = BaseClass.newCustomClassInstance(classReference, context);
                }
                if (newobject != null) {
                    newobject.setId(object.getId());
                    newobject.setXClassReference(object.getXClassReference());
                    newobject.setDocumentReference(object.getDocumentReference());
                    newobject.setNumber(object.getNumber());
                    newobject.setGuid(object.getGuid());
                    object = newobject;
                }

                if (classReference.equals(groupsDocumentReference)) {
                    // Groups objects are handled differently.
                    hasGroups = true;
                } else {
                    loadXWikiCollection(object, doc, context, false, true);
                }
                doc.setXObject(object.getNumber(), object);
            }

            // AFAICT this was added as an emergency patch because loading of objects has proven
            // too slow and the objects which cause the most overhead are the XWikiGroups objects
            // as each group object (each group member) would otherwise cost 2 database queries.
            // This will do every group member in a single query.
            if (hasGroups) {
                Query query2 = session
                        .createQuery("select bobject.number, prop.value from StringProperty as prop,"
                                + "BaseObject as bobject where bobject.name = :name and bobject.className='XWiki.XWikiGroups' "
                                + "and bobject.id=prop.id.id and prop.id.name='member' order by bobject.number");
                query2.setText("name", doc.getFullName());
                @SuppressWarnings("unchecked")
                Iterator<Object[]> it2 = query2.list().iterator();
                while (it2.hasNext()) {
                    Object[] result = it2.next();
                    Integer number = (Integer) result[0];
                    String member = (String) result[1];
                    BaseObject obj = BaseClass.newCustomClassInstance(groupsDocumentReference, context);
                    obj.setDocumentReference(doc.getDocumentReference());
                    obj.setXClassReference(localGroupEntityReference);
                    obj.setNumber(number.intValue());
                    obj.setStringValue("member", member);
                    doc.setXObject(obj.getNumber(), obj);
                }
            }
        }

        // We need to ensure that the loaded document becomes the original document
        doc.setOriginalDocument(doc.clone());

        if (bTransaction) {
            endTransaction(context, false, false);
        }
    } catch (Exception e) {
        Object[] args = { doc.getDocumentReference() };
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
                XWikiException.ERROR_XWIKI_STORE_HIBERNATE_READING_DOC,
                "Exception while reading document [{0}]", e, args);
    } finally {
        try {
            if (bTransaction) {
                endTransaction(context, false, false);
            }
        } catch (Exception e) {
        }

        // End monitoring timer
        if (monitor != null) {
            monitor.endTimer("hibernate");
        }
    }

    log.debug("Loaded XWikiDocument: " + doc.getDocumentReference());

    return doc;
}

From source file:Datos.PacienteDAO.java

public Paciente verificarExistencia(Paciente pte) {
    Paciente paciente = null;/*w  ww  .  j a v a 2s  .c  om*/

    try {
        iniciarOperacion();

        Query query = sesion.createQuery("FROM Paciente WHERE " + "apellidoPaterno=:apellidoPaterno AND "
                + "apellidoMaterno=:apellidoMaterno AND " + "nombres=:nombres AND " + "fechaNacimiento=:fecha");

        query.setText("apellidoPaterno", pte.getApellidoPaterno());
        query.setText("apellidoMaterno", pte.getApellidoMaterno());
        query.setText("nombres", pte.getNombres());
        query.setDate("fecha", pte.getFechaNacimiento());

        if (query.list().size() > 0) {
            paciente = (Paciente) query.list().get(0);
        } else {
            paciente = null;
        }
    } catch (HibernateException he) {
        manejaExcepcion(he);
    } finally {
        sesion.close();
    }

    return paciente;
}

From source file:sos.ftphistory.db.JadeFilesDBLayer.java

License:Apache License

private void setWhere(Query query) {

    if (filter.getCreatedFrom() != null && !filter.getCreatedFrom().equals("")) {
        query.setTimestamp("createdFrom", filter.getCreatedFrom());
    }//from   w w w  .  j  a  va 2s . c o  m

    if (filter.getCreatedTo() != null && !filter.getCreatedTo().equals("")) {
        query.setTimestamp("createdTo", filter.getCreatedTo());
    }

    if (filter.getSourceDir() != null && !filter.getSourceDir().equals("")) {
        query.setText("sourceDir", filter.getSourceDir());
    }

    if (filter.getSourceFilename() != null && !filter.getSourceFilename().equals("")) {
        query.setText("sourceFilename", filter.getSourceFilename());
    }

    if (filter.getSourceHost() != null && !filter.getSourceHost().equals("")) {
        query.setText("sourceHost", filter.getSourceHost());
    }

    if (filter.getSourceHostIp() != null && !filter.getSourceHostIp().equals("")) {
        query.setText("sourceHostIp", filter.getSourceHostIp());
    }

    if (filter.getSourceUser() != null && !filter.getSourceUser().equals("")) {
        query.setText("sourceUser", filter.getSourceUser());
    }

}