Example usage for org.hibernate Session setFlushMode

List of usage examples for org.hibernate Session setFlushMode

Introduction

In this page you can find the example usage for org.hibernate Session setFlushMode.

Prototype

@Deprecated
void setFlushMode(FlushMode flushMode);

Source Link

Document

Set the flush mode for this session.

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 {/*www .  ja va 2s.c  o m*/
        // 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:com.xpn.xwiki.store.XWikiHibernateStore.java

License:Open Source License

public void deleteXWikiDoc(XWikiDocument doc, XWikiContext context) throws XWikiException {
    boolean bTransaction = true;
    MonitorPlugin monitor = Util.getMonitorPlugin(context);
    try {//w ww  . j  a v a 2  s.c o m
        // Start monitoring timer
        if (monitor != null) {
            monitor.startTimer("hibernate");
        }
        checkHibernate(context);
        SessionFactory sfactory = injectCustomMappingsInSessionFactory(doc, context);
        bTransaction = bTransaction && beginTransaction(sfactory, context);
        Session session = getSession(context);
        session.setFlushMode(FlushMode.COMMIT);

        if (doc.getStore() == null) {
            Object[] args = { doc.getDocumentReference() };
            throw new XWikiException(XWikiException.MODULE_XWIKI_STORE,
                    XWikiException.ERROR_XWIKI_STORE_HIBERNATE_CANNOT_DELETE_UNLOADED_DOC,
                    "Impossible to delete document {0} if it is not loaded", null, args);
        }

        // Let's delete any attachment this document might have
        for (XWikiAttachment attachment : doc.getAttachmentList()) {
            context.getWiki().getAttachmentStore().deleteXWikiAttachment(attachment, false, context, false);
        }

        // deleting XWikiLinks
        if (context.getWiki().hasBacklinks(context)) {
            deleteLinks(doc.getId(), context, true);
        }

        // Find the list of classes for which we have an object
        // Remove properties planned for removal
        if (doc.getXObjectsToRemove().size() > 0) {
            for (BaseObject bobj : doc.getXObjectsToRemove()) {
                if (bobj != null) {
                    deleteXWikiObject(bobj, context, false);
                }
            }
            doc.setXObjectsToRemove(new ArrayList<BaseObject>());
        }
        for (List<BaseObject> objects : doc.getXObjects().values()) {
            for (BaseObject obj : objects) {
                if (obj != null) {
                    deleteXWikiObject(obj, context, false);
                }
            }
        }
        context.getWiki().getVersioningStore().deleteArchive(doc, false, context);

        session.delete(doc);

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

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

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

From source file:com.zutubi.pulse.master.xwork.interceptor.HibernateSessionInterceptor.java

License:Apache License

protected Session openSession() {
    try {/*from w w w .  ja  v  a  2  s .c o m*/
        Session session = sessionFactory.openSession();
        session.setFlushMode(FlushMode.MANUAL);
        return session;
    } catch (HibernateException ex) {
        throw new PulseRuntimeException("Could not open Hibernate Session", ex);
    }
}

From source file:com.zutubi.pulse.master.xwork.interceptor.ReadOnlyInterceptor.java

License:Apache License

public String intercept(ActionInvocation invocation) throws Exception {
    Session session = sessionFactory.getCurrentSession();
    session.setFlushMode(FlushMode.MANUAL);
    return invocation.invoke();
}

From source file:corner.orm.tapestry.filter.OneSessionPerServletRequestFilter.java

License:Apache License

/**
 * Get a Session for the SessionFactory that this filter uses. Note that
 * this just applies in single session mode!
 * <p>// w  w  w.j a  va2s.c o m
 * The default implementation delegates to SessionFactoryUtils' getSession
 * method and sets the Session's flushMode to NEVER.
 * <p>
 * Can be overridden in subclasses for creating a Session with a custom
 * entity interceptor or JDBC exception translator.
 * 
 * @param sessionFactory
 *            the SessionFactory that this filter uses
 * @return the Session to use
 * @throws DataAccessResourceFailureException
 *             if the Session could not be created
 * @see org.springframework.orm.hibernate3.SessionFactoryUtils#getSession(SessionFactory,
 *      boolean)
 * @see org.hibernate.FlushMode#NEVER
 */
protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
    Session session = SessionFactoryUtils.getSession(sessionFactory, true);
    //TODO NEVER
    session.setFlushMode(FlushMode.AUTO);
    return session;
}

From source file:cz.zcu.kiv.eegdatabase.data.persistent.HibernateFilter.java

License:Apache License

@Override
protected Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
    Session session = SessionFactoryUtils.getSession(sessionFactory, true);

    // set FlushMode to AUTO in order to save objects.
    session.setFlushMode(FlushMode.AUTO);

    return session;
}

From source file:dao.DaoCommande.java

@Override
public void insert(Metier objet) {
    System.out.println(objet);//from w ww.  j  a va2s. c  o  m
    Session session = getHibernateTemplate().getSessionFactory().openSession();
    session.setFlushMode(FlushMode.AUTO);
    session.save(objet);
    session.flush();

}

From source file:de.codesourcery.eve.skills.db.dao.HibernateDAO.java

License:Apache License

private Session getCurrentSession() {
    if (currentSession.get() == null) {
        org.hibernate.classic.Session session = sessionFactory.openSession();
        session.setFlushMode(FlushMode.NEVER);
        currentSession.set(session);/*from   ww  w .j a  va  2  s . c  om*/
    }
    return currentSession.get();
}

From source file:de.escidoc.core.common.persistence.hibernate.CustomHibernateSessionFilter.java

License:Open Source License

@Override
protected Session getSession(final SessionFactory sessionFactory) {
    final Session session = super.getSession(sessionFactory);
    session.setFlushMode(FlushMode.COMMIT);
    return session;
}

From source file:de.escidoc.core.common.util.aop.HibernateInterceptor.java

License:Open Source License

private Session getSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException {
    Session session = SessionFactoryUtils.getSession(sessionFactory, true);
    session.setFlushMode(FLUSH_MODE);
    return session;
}