Example usage for org.hibernate StatelessSession insert

List of usage examples for org.hibernate StatelessSession insert

Introduction

In this page you can find the example usage for org.hibernate StatelessSession insert.

Prototype

Serializable insert(Object entity);

Source Link

Document

Insert a row.

Usage

From source file:gov.nih.nci.cabig.caaers.dao.OrganizationDao.java

License:BSD License

public void saveAll(final List<Organization> organizations) {
    StatelessSession session = getHibernateTemplate().getSessionFactory().openStatelessSession();
    try {/*from www  . j a  v  a 2 s.  c o  m*/
        for (Organization o : organizations) {
            log.info("processing :" + o.getNciInstituteCode());
            if (o.getId() == null) {
                if (log.isInfoEnabled())
                    log.info("Inserting " + o.getNciInstituteCode());
                session.insert(o);
            } else {
                if (log.isInfoEnabled())
                    log.info("Updating " + o.getNciInstituteCode());
                session.update(o);
            }
        }
    } catch (HibernateException e) {
        throw getHibernateTemplate().convertHibernateAccessException(e);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        session.close();
    }
}

From source file:org.jahia.services.content.nodetypes.NodeTypesDBServiceImpl.java

License:Open Source License

public void saveCndFile(String filename, String content, Properties properties) throws RepositoryException {
    StatelessSession session = null;
    try {/* w w  w .j a  v a2s  .c o m*/
        session = getHibernateSessionFactory().openStatelessSession();
        session.beginTransaction();
        NodeTypesDBProvider nodeTypesDBProvider = (NodeTypesDBProvider) session
                .createQuery("from NodeTypesDBProvider where filename=:filename")
                .setString("filename", filename).setReadOnly(false).uniqueResult();
        if (nodeTypesDBProvider != null && content != null) {
            nodeTypesDBProvider.setCndFile(content);
            session.update(nodeTypesDBProvider);
        } else if (nodeTypesDBProvider != null) {
            session.delete(nodeTypesDBProvider);
        } else if (content != null) {
            nodeTypesDBProvider = new NodeTypesDBProvider();
            nodeTypesDBProvider.setFilename(filename);
            nodeTypesDBProvider.setCndFile(content);
            session.insert(nodeTypesDBProvider);
        }

        final StringWriter writer = new StringWriter();
        properties.store(writer, "");

        nodeTypesDBProvider = (NodeTypesDBProvider) session
                .createQuery("from NodeTypesDBProvider where filename=:filename")
                .setString("filename", DEFINITIONS_PROPERTIES).setReadOnly(false).uniqueResult();
        if (nodeTypesDBProvider != null) {
            nodeTypesDBProvider.setCndFile(writer.toString());
            session.update(nodeTypesDBProvider);
        } else {
            nodeTypesDBProvider = new NodeTypesDBProvider();
            nodeTypesDBProvider.setFilename(DEFINITIONS_PROPERTIES);
            nodeTypesDBProvider.setCndFile(writer.toString());
            session.insert(nodeTypesDBProvider);
        }

        session.getTransaction().commit();
    } catch (Exception e) {
        if (session != null) {
            session.getTransaction().rollback();
        }
        throw new RepositoryException(e);
    } finally {
        if (session != null) {
            session.close();
        }
    }
}

From source file:org.openremote.beehive.api.service.impl.GenericDAO.java

License:Open Source License

/**
 * Inserts a batch of given persistent instances.
 * //from   ww w  . j  a v  a2s . c  om
 * @param <T>
 *           a persistent generics
 * @param insertList
 *           persistent instances to insert.
 */
public <T> void batchInsert(final List<T> insertList) {
    StatelessSession statelessSession = getSessionFactory().openStatelessSession();
    try {
        for (T o : insertList) {
            statelessSession.insert(o);
        }
    } catch (HibernateException ex) {
        throw convertHibernateAccessException(ex);
    } catch (RuntimeException ex) {
        throw ex;
    } finally {
        closeStatelessSession(statelessSession);
    }
}

From source file:org.openspaces.persistency.hibernate.StatelessHibernateExternalDataSource.java

License:Open Source License

private void executeWriteIfExists(StatelessSession session, BulkItem bulkItem) {
    Object entry = bulkItem.getItem();
    if (exists(bulkItem, session)) {
        if (logger.isTraceEnabled()) {
            logger.trace("[Exists WRITE] Update Entry [" + entry + "]");
        }/*from   w  w  w .  jav a2 s  .c  om*/
        session.update(entry);
    } else {
        if (logger.isTraceEnabled()) {
            logger.trace("[Exists WRITE] Insert Entry [" + entry + "]");
        }
        session.insert(entry);
    }
}

From source file:org.openspaces.persistency.hibernate.StatelessHibernateExternalDataSource.java

License:Open Source License

private void executeUpdateIfExists(StatelessSession session, BulkItem bulkItem) {
    Object entry = bulkItem.getItem();
    if (exists(bulkItem, session)) {
        if (logger.isTraceEnabled()) {
            logger.trace("[Exists UPDATE] Update Entry [" + entry + "]");
        }//from w w  w  .j  av  a 2s  . c o  m
        session.update(entry);
    } else {
        if (logger.isTraceEnabled()) {
            logger.trace("[Exists UPDATE] Insert Entry [" + entry + "]");
        }
        session.insert(entry);
    }
}

From source file:org.openspaces.persistency.hibernate.StatelessHibernateExternalDataSource.java

License:Open Source License

private void executeWrite(StatelessSession session, BulkItem bulkItem) {
    Object entry = bulkItem.getItem();

    if (logger.isTraceEnabled()) {
        logger.trace("[Optimistic WRITE] Write Entry [" + entry + "]");
    }// w ww . j  a  v  a 2s  .  c om
    session.insert(entry);
}

From source file:org.openspaces.persistency.hibernate.StatelessHibernateSpaceSynchronizationEndpoint.java

License:Open Source License

private void executeWriteIfExists(StatelessSession session, DataSyncOperation dataSyncOperation) {
    if (!dataSyncOperation.supportsDataAsObject())
        return;/*from w  w w  .j  ava  2 s.  c  o m*/

    Object entry = dataSyncOperation.getDataAsObject();
    if (exists(dataSyncOperation, session)) {
        if (logger.isTraceEnabled()) {
            logger.trace("[Exists WRITE] Update Entry [" + entry + "]");
        }
        session.update(entry);
    } else {
        if (logger.isTraceEnabled()) {
            logger.trace("[Exists WRITE] Insert Entry [" + entry + "]");
        }
        session.insert(entry);
    }
}

From source file:org.openspaces.persistency.hibernate.StatelessHibernateSpaceSynchronizationEndpoint.java

License:Open Source License

private void executeUpdateIfExists(StatelessSession session, DataSyncOperation dataSyncOperation) {
    if (!dataSyncOperation.supportsDataAsObject())
        return;//from   w  w  w.  java 2 s  .c  om

    Object entry = dataSyncOperation.getDataAsObject();
    if (exists(dataSyncOperation, session)) {
        if (logger.isTraceEnabled()) {
            logger.trace("[Exists UPDATE] Update Entry [" + entry + "]");
        }
        session.update(entry);
    } else {
        if (logger.isTraceEnabled()) {
            logger.trace("[Exists UPDATE] Insert Entry [" + entry + "]");
        }
        session.insert(entry);
    }
}

From source file:org.openspaces.persistency.hibernate.StatelessHibernateSpaceSynchronizationEndpoint.java

License:Open Source License

private void executeWrite(StatelessSession session, DataSyncOperation dataSyncOperation) {
    if (!dataSyncOperation.supportsDataAsObject())
        return;// w  ww .j  av  a2 s  . c  om

    Object entry = dataSyncOperation.getDataAsObject();

    if (logger.isTraceEnabled()) {
        logger.trace("[Optimistic WRITE] Write Entry [" + entry + "]");
    }
    session.insert(entry);
}

From source file:org.transitime.misc.HibernateTest.java

License:Open Source License

/**
 * For comparing timing of doing a large number of inserts. Can either
 * use regular batch mode where there is a specified batch size but
 * the caching and other internal features are still used. Or can
 * use a StatelessSession to bypass all of that internal stuff.
 * This way can easily determine which is the best method.
 * @param cnt// w w w  . jav a 2s .c o m
 * @param batch
 */
private static void timeBatchStore(int cnt, boolean batch) {
    System.out.println((batch ? "Batch" : "Stateless") + " storing " + cnt + " records");
    IntervalTimer timer = null;

    try {
        SessionFactory sessionFactory = HibernateUtils.getSessionFactory("test");

        // Start timer after session factory is obtained since first time it is
        // obtained it takes a long time. This timer is used to determine how long
        // things take to run.
        timer = new IntervalTimer();

        // Get ready to write
        StatelessSession statelessSession = sessionFactory.openStatelessSession();
        Session batchSession = sessionFactory.openSession();
        System.out.println("Opening session took " + timer.elapsedMsec() + " msec");
        try {
            Transaction tx = batch ? batchSession.beginTransaction() : statelessSession.beginTransaction();

            // Store some AVLReportss
            long initialTime = System.currentTimeMillis();
            for (int i = 0; i < cnt; ++i) {
                AvlReport report = new AvlReport((batch ? "batch" : "stateless") + i, initialTime + i, 1.23,
                        4.56, null);
                if (batch)
                    batchSession.save(report);
                else
                    statelessSession.insert(report);
                if (batch && ((i + 1) % BATCH_SIZE == 0)) {
                    batchSession.flush();
                    batchSession.clear();
                }
            }

            // Finish up writing
            tx.commit();
        } catch (Exception e) {
            // If should rollback the transaction it would be done here
            e.printStackTrace();
        } finally {
            // No matter what, close the session
            batchSession.close();
        }
    } catch (HibernateException e) {
        e.printStackTrace();
    }

    System.out.println((batch ? "Batch" : "Stateless") + " storing " + cnt + " records took "
            + timer.elapsedMsec() + " msec");
}