Example usage for org.hibernate SessionFactory getCurrentSession

List of usage examples for org.hibernate SessionFactory getCurrentSession

Introduction

In this page you can find the example usage for org.hibernate SessionFactory getCurrentSession.

Prototype

Session getCurrentSession() throws HibernateException;

Source Link

Document

Obtains the current session.

Usage

From source file:edu.rmit.eres.seaports.database.TradeDataSourceLoader.java

License:Open Source License

/**
 * Main method used to load  the CSIRO Data Source only.
 * On an existing database, this may duplicate data.
 * @param args: no parameters//w w  w.  j a v a  2s  . co  m
 */
public static void main(String[] args) {
    AnnotationConfiguration config = new AnnotationConfiguration();
    config.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
    config.configure("hibernate.cfg.xml");
    new SchemaExport(config).create(true, true);

    SessionFactory factory = config.buildSessionFactory();
    Session session = factory.getCurrentSession();
    session.beginTransaction();

    LoadTradeDataSource(session);

    session.getTransaction().commit();
}

From source file:edu.utah.further.ds.impl.executor.db.hibernate.criteria.HibernateLoadByIdExecutor.java

License:Apache License

/**
 * @param request/*from  ww  w.  ja  v a 2  s  .c o  m*/
 * @return
 * @see edu.utah.further.core.chain.AbstractRequestHandler#process(edu.utah.further.core.api.chain.ChainRequest)
 */
@Override
public boolean process(final ChainRequest request) {
    // Read input arguments
    final HibernateExecReq executionReq = new HibernateExecReq(request);
    final SessionFactory sessionFactory = executionReq.getSessionFactory();
    notNull(sessionFactory, "Expected SessionFactory");

    final Class<? extends PersistentEntity<?>> rootEntity = executionReq.getRootEntity();
    notNull(rootEntity, "Expected root entity class");

    // Read the search criteria's root entity meta data
    final List<Object> list = executionReq.getResult();
    final Object[] listArray = CollectionUtil.toArrayNullSafe(list);
    final ClassMetadata classMetadata = sessionFactory.getClassMetadata(rootEntity);
    final String identifierName = classMetadata.getIdentifierPropertyName();
    final Type identifierType = classMetadata.getIdentifierType();
    final int numTypes = listArray.length;
    final Type[] types = new Type[numTypes];
    for (int i = 0; i < numTypes; i++) {
        types[i] = identifierType;
    }

    // Build Hibernate criteria
    final GenericCriteria criteria = GenericCriteriaFactory.criteria(CriteriaType.CRITERIA, rootEntity,
            sessionFactory.getCurrentSession());
    if (identifierType.isComponentType()) {
        final String sqlInClause = HibernateUtil.sqlRestrictionCompositeIn(rootEntity, sessionFactory,
                numTypes);
        criteria.add(Restrictions.sqlRestriction(sqlInClause, listArray, types));
    } else {
        final int size = list.size();
        if (size > MAX_IN) {
            // Create a disjunction of IN clauses. Add MAX_IN elements at a time to
            // each IN clause (except the last IN, whose size is size % MAX_IN).
            final Junction junction = Restrictions.disjunction();
            for (int i = 0; i < size; i += MAX_IN) {
                junction.add(
                        Restrictions.in(THIS + identifierName, list.subList(i, Math.max(size, MAX_IN + i))));
            }
            criteria.add(junction);
        } else {
            // Single chunk, add directly as a criterion without the junction trick
            criteria.add(Restrictions.in(THIS + identifierName, list));
        }

    }

    executionReq.setResult(criteria);
    return false;
}

From source file:edu.utah.further.ds.impl.executor.db.hibernate.criteria.HibernateResultListRefresher.java

License:Apache License

@Override
public boolean process(final ChainRequest request) {
    if (log.isDebugEnabled()) {
        log.debug("Re-reading entity state by calling refresh");
    }/*from w w w  .j  av  a 2 s . c  o  m*/

    final List<?> results = request.getAttribute(QUERY_RESULT);
    Validate.notNull(results, "Expected query results");
    final HibernateExecReq executionReq = new HibernateExecReq(request);
    final SessionFactory sessionFactory = executionReq.getSessionFactory();
    Validate.notNull(sessionFactory, "SessionFactory required to re-read entity state");

    final Session session = sessionFactory.getCurrentSession();
    for (final Object result : results) {
        if (log.isTraceEnabled()) {
            log.trace("Before refreshing " + result);
        }

        session.refresh(result);

        if (log.isTraceEnabled()) {
            log.trace("After refreshing " + result);
        }
    }

    if (log.isDebugEnabled()) {
        log.debug("All entities refreshed");
    }

    return false;
}

From source file:edu.utah.further.ds.impl.service.query.processor.PageFinalizerHibernateQp.java

License:Apache License

/**
 * @param request/*from w  ww .  j a  va  2  s  . c  o m*/
 * @return
 * @see edu.utah.further.core.api.chain.RequestProcessor#process(edu.utah.further.core.api.chain.ChainRequest)
 */
@Override
public boolean process(final ChainRequest request) {
    // Read inputs from chain request
    final QueryContext queryContext = request.getAttribute(QUERY_CONTEXT);
    final List<?> resultPage = request.getAttribute(QUERY_RESULT);
    final PagingLoopController controller = request.getAttribute(AttributeName.PAGING_LOOP_CONTROLLER);

    // Perform finalization logic
    // In the future pass only selected attributes to the delegate. For simplicity,
    // pass all of them for now.
    final QueryContext newQueryContext = getDelegate().finalizePage(queryContext, resultPage, controller);

    // Clear persistent session
    final SessionFactory sessionFactory = request.getAttribute(SESSION_FACTORY);
    final Session session = sessionFactory.getCurrentSession();
    // Flush the current page's batch of reads, inserts and updates and release memory
    session.flush();
    session.clear();

    // Save results in the chain request
    request.setAttribute(QUERY_CONTEXT, newQueryContext);

    return false;
}

From source file:eionet.webq.dao.FileContentUtil.java

License:Mozilla Public License

private static int countRows(SessionFactory factory, String tableName) {
    BigInteger count = (BigInteger) factory.getCurrentSession()
            .createSQLQuery("select count(*) from " + tableName).uniqueResult();
    return count.intValue();
}

From source file:fr.alpha.actions.DemandeReparationAction.java

public String listCategories() {
    SessionFactory factory = HibernateUtil.createSessionFactory();
    categorieDAO.setSessionFactory(factory);
    Transaction tx = factory.getCurrentSession().beginTransaction();
    categories = categorieDAO.findAll();
    //tx.commit();
    return INPUT;
}

From source file:fr.alpha.actions.DemandeReparationAction.java

public String listModels() {
    SessionFactory factory = HibernateUtil.createSessionFactory();
    modelDAO.setSessionFactory(factory);
    Transaction tx = factory.getCurrentSession().beginTransaction();
    models = modelDAO.findByCategory(yourCategory);
    tx.commit();/* ww  w  .  j  ava 2s . c  o m*/

    return INPUT;
}

From source file:fr.alpha.actions.DemandeReparationAction.java

public String listForfaits() {
    SessionFactory factory = HibernateUtil.createSessionFactory();
    forfaitDAO.setSessionFactory(factory);
    Transaction tx = factory.getCurrentSession().beginTransaction();
    forfaits = forfaitDAO.findByCategory(yourCategory);
    tx.commit();//w ww.ja va  2 s.  c o  m

    System.out.println(forfaits);

    return INPUT;
}

From source file:fr.alpha.actions.DemandeReparationAction.java

public String listVendeurs() {
    SessionFactory factory = HibernateUtil.createSessionFactory();
    vendeurDAO.setSessionFactory(factory);
    Transaction tx = factory.getCurrentSession().beginTransaction();
    vendeurs = vendeurDAO.findByCode(863752);
    tx.commit();/*  w w w  .j  a  va  2  s. c  o  m*/
    return INPUT;
}

From source file:fr.alpha.actions.DemandeReparationAction.java

public String recapitulatif() {
    SessionFactory factory = HibernateUtil.createSessionFactory();
    forfaitDAO.setSessionFactory(factory);
    Transaction tx = factory.getCurrentSession().beginTransaction();
    forfait = forfaitDAO.find(yourForfait);
    modelDAO.setSessionFactory(factory);
    Modele modele = modelDAO.find(yourModel);

    session.put("demande", new DemandeReparation(modele, forfait, description, ville));

    return SUCCESS;
}