Example usage for org.hibernate Session getSessionFactory

List of usage examples for org.hibernate Session getSessionFactory

Introduction

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

Prototype

SessionFactory getSessionFactory();

Source Link

Document

Get the session factory which created this session.

Usage

From source file:com.hmsinc.epicenter.model.util.ModelUtils.java

License:Open Source License

/**
 * Disables "nested loops" optimization on PostgreSQL. This is needed with
 * various spatial queries that confuse the optimizer.
 * /*from   w  w  w  .  j a  v a 2s  .  co  m*/
 * @param entityManager
 */
public static void disableNestedLoops(final EntityManager entityManager) {

    final Session s = (Session) entityManager.getDelegate();
    final SessionFactoryImpl sf = (SessionFactoryImpl) s.getSessionFactory();
    if (sf.getDialect() instanceof PostgreSQLDialect) {
        s.doWork(new Work() {
            public void execute(Connection conn) throws SQLException {
                conn.createStatement().execute("set enable_nestloop=off");
            }

        });
    }
}

From source file:com.hmsinc.epicenter.model.util.ModelUtils.java

License:Open Source License

/**
 * Enables "nested loops" optimization on PostgreSQL. This is needed with
 * various spatial queries that confuse the optimizer.
 * // www.  j  a va2 s.  c  om
 * @param entityManager
 */
public static void enableNestedLoops(final EntityManager entityManager) {

    final Session s = (Session) entityManager.getDelegate();
    final SessionFactoryImpl sf = (SessionFactoryImpl) s.getSessionFactory();
    if (sf.getDialect() instanceof PostgreSQLDialect) {
        s.doWork(new Work() {
            public void execute(Connection conn) throws SQLException {
                conn.createStatement().execute("set enable_nestloop=on");
            }

        });
    }
}

From source file:com.iatai.ejbpersistencemaven.persistence.dao.base.BaseDao.java

/**
 * Gets the connection./* w  ww.  ja v a  2s . c  o m*/
 *
 * @param entityManager the entity manager
 * @return the connection
 * @throws InfraestructureException the persistence exception
 */
public Connection getConnection(EntityManager entityManager) throws InfraestructureException {
    Connection con = null;
    try {
        Session session = (Session) entityManager.getDelegate();
        SessionFactoryImplementor sfi = (SessionFactoryImplementor) session.getSessionFactory();
        ConnectionProvider cp = sfi.getConnectionProvider();
        con = cp.getConnection();
    } catch (SQLException sqle) {
        new InfraestructureException("BaseDao.getConnection: SQLException: " + sqle.getMessage(), sqle);
    } catch (Exception e) {
        new InfraestructureException("BaseDao.getConnection: Exception: " + e.getMessage(), e);
    }
    return con;

}

From source file:com.javaetmoi.core.persistence.hibernate.LazyLoadingUtil.java

License:Apache License

@SuppressWarnings("unchecked")
private static void deepInflateEntity(final Session currentSession, Object entity, Set<String> recursiveGuard)
        throws HibernateException {
    if (entity == null) {
        return;//from  w ww  . j av  a 2s.  c  o  m
    }

    Class<? extends Object> persistentClass = entity.getClass();
    if (entity instanceof HibernateProxy) {
        persistentClass = ((HibernateProxy) entity).getHibernateLazyInitializer().getPersistentClass();
    }
    ClassMetadata classMetadata = currentSession.getSessionFactory().getClassMetadata(persistentClass);
    if (classMetadata == null) {
        return;
    }
    Serializable identifier = classMetadata.getIdentifier(entity, (AbstractSessionImpl) currentSession);
    String key = persistentClass.getName() + "|" + identifier;

    if (recursiveGuard.contains(key)) {
        return;
    }
    recursiveGuard.add(key);

    if (!Hibernate.isInitialized(entity)) {
        Hibernate.initialize(entity);
    }

    for (int i = 0, n = classMetadata.getPropertyNames().length; i < n; i++) {
        String propertyName = classMetadata.getPropertyNames()[i];
        Type propertyType = classMetadata.getPropertyType(propertyName);
        Object propertyValue = null;
        if (entity instanceof javassist.util.proxy.ProxyObject) {
            // For javassist proxy, the classMetadata.getPropertyValue(..) method return en
            // emppty collection. So we have to call the property's getter in order to call the
            // JavassistLazyInitializer.invoke(..) method that will initialize the collection by
            // loading it from the database.
            propertyValue = callCollectionGetter(entity, propertyName);
        } else {
            propertyValue = classMetadata.getPropertyValue(entity, propertyName, EntityMode.POJO);
        }
        deepInflateProperty(propertyValue, propertyType, currentSession, recursiveGuard);
    }
}

From source file:com.javaetmoi.core.persistence.hibernate.LazyLoadingUtil.java

License:Apache License

/**
 * //from w ww .ja v  a  2 s  . c  o m
 * 
 * @param sessionFactory
 * @param recursiveGuard
 * @param collection
 */
private static void deepInflateCollection(Session currentSession, Set<String> recursiveGuard,
        @SuppressWarnings("rawtypes") Collection collection) {
    if (collection != null && collection.size() > 0) {
        ComponentType collectionType = null;
        if (collection instanceof PersistentCollection
                && !((PersistentCollection) collection).isUnreferenced()) {
            String role = ((PersistentCollection) collection).getRole();
            Type type = currentSession.getSessionFactory().getCollectionMetadata(role).getElementType();
            if (type instanceof ComponentType) {
                // ManyToMany relationship with @Embeddable annotation (see
                // https://github.com/arey/hibernate-hydrate/issues/3)
                collectionType = (ComponentType) type;
            }
        }
        for (Object item : collection) {
            if (item == null) {
                continue;
            } else if (collectionType != null) {
                deepInflateComponent(currentSession, item, collectionType, recursiveGuard);
            } else {
                deepInflateEntity(currentSession, item, recursiveGuard);
            }
        }
    }
}

From source file:com.jfootball.dao.hibernate.PlayerDaoImpl.java

License:Open Source License

public void endSeasonJob() {
    logger.info("Execute endSeason job");

    Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
    SessionFactoryImpl sessionFactory = (SessionFactoryImpl) session.getSessionFactory();

    try {//from w w w .j a va 2 s  . c  o m
        Connection conn = sessionFactory.getConnectionProvider().getConnection();
        CallableStatement cstmt = conn.prepareCall("{ call endSeasonBatch(?) }");

        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(new Date(System.currentTimeMillis()));

        int year = gc.get(Calendar.YEAR);

        cstmt.setString("param_year", "30/06/" + year); // current year.

        cstmt.execute();

        logger.info("EndSeason job executed");
    } catch (SQLException e) {
        logger.error(e);
    }

    logger.info("EndSeason job finished");
}

From source file:com.jfootball.dao.hibernate.PlayerDaoImpl.java

License:Open Source License

public void careerPlayerJob() {
    logger.info("Fix CareerPlayer job");

    Session session = hibernateTemplate.getSessionFactory().getCurrentSession();
    SessionFactoryImpl sessionFactory = (SessionFactoryImpl) session.getSessionFactory();

    try {//ww w  . j a  v a  2  s .  c  om
        Connection conn = sessionFactory.getConnectionProvider().getConnection();
        CallableStatement cstmt = conn.prepareCall("{ call careerPlayerBatch() }");
        cstmt.execute();
    } catch (SQLException e) {
        logger.error(e);
    }
    logger.info("Fix CareerPlayer job finished");
}

From source file:com.lp.server.artikel.ejbfac.LagerFacBean.java

License:Open Source License

private BigDecimal recalcGestehungspreisAbTermin(Integer bewegungIId, Integer artikelIId, Integer lagerIId,
        Timestamp startTermin, TheClientDto theClientDto) {

    // Ohne Uhrzeit, da Lagerabgaenge keine Uhrzeit im Belegdatum haben.
    startTermin = Helper.cutTimestamp(startTermin);

    // wegen vordatierter Abbuchungen pruefen
    Timestamp tVordatiert = getAbbuchungenVordatiert(bewegungIId, startTermin);
    if (tVordatiert.getTime() < startTermin.getTime()) {
        startTermin = tVordatiert;/*from www  .  j a v  a  2  s . c o  m*/
        startTermin = Helper.cutTimestamp(startTermin);
    }
    Session session = FLRSessionFactory.getFactory().openSession();
    Dialect dialect = ((SessionFactoryImplementor) session.getSessionFactory()).getDialect();
    String sQuery = null;
    if (dialect instanceof org.hibernate.dialect.SQLServerDialect) {
        sQuery = "select  I_ID, B_ABGANG, SUM(N_MENGE) as N_MENGE, N_EINSTANDSPREIS, N_GESTEHUNGSPREIS, LAGER_I_ID, CONVERT(CHAR(10), T_BELEGDATUM, 102) AS T_BELEGDATUM, T_BUCHUNGSZEIT "
                + "from WW_LAGERBEWEGUNG "
                + "where LAGER_I_ID=? and ARTIKEL_I_ID=? and B_ABGANG=0 and B_HISTORIE=0 and T_BELEGDATUM >= ? "
                + "group by LAGER_I_ID, CONVERT(CHAR(10), T_BELEGDATUM, 102), T_BUCHUNGSZEIT, B_ABGANG, I_ID, N_EINSTANDSPREIS, N_GESTEHUNGSPREIS "
                + "union "
                + "select I_ID, B_ABGANG, SUM(U.N_VERBRAUCHTEMENGE) as MENGE, N_EINSTANDSPREIS, U.N_GESTEHUNGSPREIS, LAGER_I_ID, CONVERT(CHAR(10), B.T_BELEGDATUM, 102) AS T_BELEGDATUM, T_BUCHUNGSZEIT "
                + "from WW_LAGERBEWEGUNG B inner join WW_LAGERABGANGURSPRUNG U on U.I_LAGERBEWEGUNGID = B.I_ID_BUCHUNG "
                + " where LAGER_I_ID=?  and ARTIKEL_I_ID=? and B_ABGANG=1 and B_HISTORIE=0 and T_BELEGDATUM >= ? "
                + "group by LAGER_I_ID, CONVERT(CHAR(10), T_BELEGDATUM, 102), B.T_BUCHUNGSZEIT, B_ABGANG, B.I_ID, N_EINSTANDSPREIS, U.N_GESTEHUNGSPREIS "
                + "order by LAGER_I_ID, T_BELEGDATUM, T_BUCHUNGSZEIT, B_ABGANG";
    } else {
        sQuery = "select  I_ID, B_ABGANG, SUM(N_MENGE) as N_MENGE, N_EINSTANDSPREIS, N_GESTEHUNGSPREIS, LAGER_I_ID, CAST(T_BELEGDATUM AS CHAR(10)) AS T_BELEGDATUM, T_BUCHUNGSZEIT "
                + "from WW_LAGERBEWEGUNG "
                + "where LAGER_I_ID=? and ARTIKEL_I_ID=? and B_ABGANG=0 and B_HISTORIE=0 and T_BELEGDATUM >= ? "
                + "group by LAGER_I_ID, CAST(T_BELEGDATUM AS CHAR(10)), T_BUCHUNGSZEIT, B_ABGANG, I_ID, N_EINSTANDSPREIS, N_GESTEHUNGSPREIS "
                + "union "
                + "select I_ID, B_ABGANG, SUM(U.N_VERBRAUCHTEMENGE) as MENGE, N_EINSTANDSPREIS, U.N_GESTEHUNGSPREIS, LAGER_I_ID, CAST(T_BELEGDATUM AS CHAR(10)) AS T_BELEGDATUM, T_BUCHUNGSZEIT "
                + "from WW_LAGERBEWEGUNG B inner join WW_LAGERABGANGURSPRUNG U on U.I_LAGERBEWEGUNGID = B.I_ID_BUCHUNG "
                + " where LAGER_I_ID=?  and ARTIKEL_I_ID=? and B_ABGANG=1 and B_HISTORIE=0 and T_BELEGDATUM >= ? "
                + "group by LAGER_I_ID, CAST(B.T_BELEGDATUM AS CHAR(10)), B.T_BUCHUNGSZEIT, B_ABGANG, B.I_ID, N_EINSTANDSPREIS, U.N_GESTEHUNGSPREIS "
                + "order by LAGER_I_ID, T_BELEGDATUM, T_BUCHUNGSZEIT, B_ABGANG";
    }

    org.hibernate.Query query = session.createSQLQuery(sQuery);
    query.setParameter(0, lagerIId);
    query.setParameter(1, artikelIId);
    query.setParameter(2, startTermin);
    query.setParameter(3, lagerIId);
    query.setParameter(4, artikelIId);
    query.setParameter(5, startTermin);
    List<?> list = query.list();

    ArtikelGestehungspreisCalc agc = new ArtikelGestehungspreisCalc(list);
    session.close();

    BigDecimal bdLsZeitpunkt = getLagerstandVorZeitpunktBelegdatum(artikelIId, lagerIId, startTermin,
            theClientDto);
    BigDecimal bdGestpreisZeitpunkt = getGestehungspreisVorZeitpunktBelegdatum(artikelIId, lagerIId,
            startTermin, theClientDto);

    // agc.save2Csv("c:/temp/" + artikelIId.toString() + "_v_" + new
    // Long(System.currentTimeMillis()).toString() + ".csv");
    agc.doRecalc(lagerIId, bdLsZeitpunkt, bdGestpreisZeitpunkt.multiply(bdLsZeitpunkt), bdGestpreisZeitpunkt);

    // agc.save2Csv("c:/temp/" + artikelIId.toString() + ".csv");
    // agc.saveUpdateSQL("c:/temp/" + artikelIId.toString() + ".sql");
    // agc.save2Csv("c:/temp/" + artikelIId.toString() + "_n_" + new
    // Long(System.currentTimeMillis()).toString() + ".csv");
    /*
     * if (agc.hasDataError()) { //
     * ERSTE_BUCHUNG_ZUM_ZEITPUNKT_MUSS_ZUGANGSBUCHUNG_SEIN // TODO:
     * Aufrufer sollte Fehler fangen throw new
     * EJBExceptionLP(EJBExceptionLP.FEHLER_DATEN_INKOMPATIBEL,
     * "Erste Buchung muss eine Zugangsbuchung sein"); }
     */
    if (agc.hasGestPreisDiff() && !agc.hasOverflow()) {
        // ES GIBT EINE DIFFERENZ UPDATE AUSFUEHREN
        Iterator<String> it = agc.iteratorSql();
        session = FLRSessionFactory.getFactory().openSession();
        while (it.hasNext()) {
            String sql = it.next();
            session.createSQLQuery(sql).executeUpdate();
        }
        session.close();
    }

    if (agc.hasOverflow()) {
        return null;
    }

    return agc.getGestehungspreisNeu();
}

From source file:com.lp.server.artikel.ejbfac.LagerReportFacBean.java

License:Open Source License

public BigDecimal recalcGestehungspreisKomplett(Integer artikelIId, boolean debugFile) {

    Session session = FLRSessionFactory.getFactory().openSession();
    Dialect dialect = ((SessionFactoryImplementor) session.getSessionFactory()).getDialect();
    String sQuery = null;//from   w w w.  ja va  2 s. com
    if (dialect instanceof org.hibernate.dialect.SQLServerDialect) {
        sQuery = "select  I_ID, B_ABGANG, SUM(N_MENGE) as N_MENGE, N_EINSTANDSPREIS, N_GESTEHUNGSPREIS, LAGER_I_ID, T_BUCHUNGSZEIT, CONVERT(CHAR(10), T_BELEGDATUM, 102) AS T_BELEGDATUM "
                + "from WW_LAGERBEWEGUNG " + "where ARTIKEL_I_ID=? and B_ABGANG=0 and B_HISTORIE=0 "
                + "group by LAGER_I_ID, CONVERT(CHAR(10), T_BELEGDATUM, 102), T_BUCHUNGSZEIT, I_ID, B_ABGANG, N_EINSTANDSPREIS, N_GESTEHUNGSPREIS "
                + "union "
                + "select I_ID, B_ABGANG, SUM(U.N_VERBRAUCHTEMENGE) as MENGE, N_EINSTANDSPREIS, U.N_GESTEHUNGSPREIS, LAGER_I_ID, T_BUCHUNGSZEIT, CONVERT(CHAR(10), B.T_BELEGDATUM, 102) AS T_BELEGDATUM "
                + "from WW_LAGERBEWEGUNG B inner join WW_LAGERABGANGURSPRUNG U on U.I_LAGERBEWEGUNGID = B.I_ID_BUCHUNG "
                + " where ARTIKEL_I_ID=? and B_ABGANG=1 and B_HISTORIE=0 "
                + "group by LAGER_I_ID, CONVERT(CHAR(10), B.T_BELEGDATUM, 102), B.T_BUCHUNGSZEIT, B.I_ID, B_ABGANG, N_EINSTANDSPREIS, U.N_GESTEHUNGSPREIS "
                + "order by LAGER_I_ID, T_BELEGDATUM, T_BUCHUNGSZEIT, B_ABGANG";
    } else {
        sQuery = "select  I_ID, B_ABGANG, SUM(N_MENGE) as N_MENGE, N_EINSTANDSPREIS, N_GESTEHUNGSPREIS, LAGER_I_ID, T_BUCHUNGSZEIT, CAST(T_BELEGDATUM AS CHAR(10)) AS T_BELEGDATUM "
                + "from WW_LAGERBEWEGUNG " + "where ARTIKEL_I_ID=? and B_ABGANG=0 and B_HISTORIE=0 "
                + "group by LAGER_I_ID, CAST(T_BELEGDATUM AS CHAR(10)), T_BUCHUNGSZEIT, I_ID, B_ABGANG, N_EINSTANDSPREIS, N_GESTEHUNGSPREIS "
                + "union "
                + "select I_ID, B_ABGANG, SUM(U.N_VERBRAUCHTEMENGE) as MENGE, N_EINSTANDSPREIS, U.N_GESTEHUNGSPREIS, LAGER_I_ID, T_BUCHUNGSZEIT, CAST(B.T_BELEGDATUM AS CHAR(10)) AS T_BELEGDATUM "
                + "from WW_LAGERBEWEGUNG B inner join WW_LAGERABGANGURSPRUNG U on U.I_LAGERBEWEGUNGID = B.I_ID_BUCHUNG "
                + " where ARTIKEL_I_ID=? and B_ABGANG=1 and B_HISTORIE=0 "
                + "group by LAGER_I_ID, CAST(B.T_BELEGDATUM AS CHAR(10)), B.T_BUCHUNGSZEIT, B.I_ID, B_ABGANG, N_EINSTANDSPREIS, U.N_GESTEHUNGSPREIS "
                + "order by LAGER_I_ID, T_BELEGDATUM, T_BUCHUNGSZEIT, B_ABGANG";
    }
    org.hibernate.Query query = session.createSQLQuery(sQuery);
    query.setParameter(0, artikelIId);
    query.setParameter(1, artikelIId);
    List<?> list = query.list();
    ArtikelGestehungspreisCalc agc = new ArtikelGestehungspreisCalc(list);
    int lageranzahl = agc.getLageranzahl();
    agc.doRecalc();
    if (debugFile) {
        agc.saveUpdateSQL("c:/temp/gp" + artikelIId + ".sql");
        agc.save2Csv("c:/temp/gp" + artikelIId + ".csv");
        agc.saveInfo("c:/temp/gp" + artikelIId + ".info");
    }
    return agc.getGestehungspreisNeu();
}

From source file:com.lp.server.artikel.ejbfac.LagerReportFacBean.java

License:Open Source License

private void recalcGestehungspreisKomplett(Integer artikelIId, Integer lagerIId, Session session,
        boolean debugFile) {
    Dialect dialect = ((SessionFactoryImplementor) session.getSessionFactory()).getDialect();
    String sQuery = null;// w ww. j av a2  s . c  om
    if (dialect instanceof org.hibernate.dialect.SQLServerDialect) {
        sQuery = "select  I_ID, B_ABGANG, SUM(N_MENGE) as N_MENGE, N_EINSTANDSPREIS, N_GESTEHUNGSPREIS, LAGER_I_ID, T_BUCHUNGSZEIT, CONVERT(CHAR(10), T_BELEGDATUM, 102) AS T_BELEGDATUM "
                + "from WW_LAGERBEWEGUNG "
                + "where LAGER_I_ID=? and ARTIKEL_I_ID=? and B_ABGANG=0 and B_HISTORIE=0 "
                + "group by LAGER_I_ID, CONVERT(CHAR(10), T_BELEGDATUM, 102), T_BUCHUNGSZEIT, I_ID, B_ABGANG, N_EINSTANDSPREIS, N_GESTEHUNGSPREIS "
                + "union "
                + "select I_ID, B_ABGANG, SUM(U.N_VERBRAUCHTEMENGE) as MENGE, N_EINSTANDSPREIS, U.N_GESTEHUNGSPREIS, LAGER_I_ID, T_BUCHUNGSZEIT, CONVERT(CHAR(10), B.T_BELEGDATUM, 102) AS T_BELEGDATUM "
                + "from WW_LAGERBEWEGUNG B inner join WW_LAGERABGANGURSPRUNG U on U.I_LAGERBEWEGUNGID = B.I_ID_BUCHUNG "
                + " where LAGER_I_ID=? and ARTIKEL_I_ID=? and B_ABGANG=1 and B_HISTORIE=0 "
                + "group by LAGER_I_ID, CONVERT(CHAR(10), B.T_BELEGDATUM, 102), B.T_BUCHUNGSZEIT, B.I_ID, B_ABGANG, N_EINSTANDSPREIS, U.N_GESTEHUNGSPREIS "
                + "order by LAGER_I_ID, T_BELEGDATUM, T_BUCHUNGSZEIT, B_ABGANG";
    } else {
        sQuery = "select  I_ID, B_ABGANG, SUM(N_MENGE) as N_MENGE, N_EINSTANDSPREIS, N_GESTEHUNGSPREIS, LAGER_I_ID, T_BUCHUNGSZEIT, CAST(T_BELEGDATUM AS CHAR(10)) AS T_BELEGDATUM "
                + "from WW_LAGERBEWEGUNG "
                + "where LAGER_I_ID=? and ARTIKEL_I_ID=? and B_ABGANG=0 and B_HISTORIE=0 "
                + "group by LAGER_I_ID, CAST(T_BELEGDATUM AS CHAR(10)), T_BUCHUNGSZEIT, I_ID, B_ABGANG, N_EINSTANDSPREIS, N_GESTEHUNGSPREIS "
                + "union "
                + "select I_ID, B_ABGANG, SUM(U.N_VERBRAUCHTEMENGE) as MENGE, N_EINSTANDSPREIS, U.N_GESTEHUNGSPREIS, LAGER_I_ID, T_BUCHUNGSZEIT, CAST(B.T_BELEGDATUM AS CHAR(10)) AS T_BELEGDATUM "
                + "from WW_LAGERBEWEGUNG B inner join WW_LAGERABGANGURSPRUNG U on U.I_LAGERBEWEGUNGID = B.I_ID_BUCHUNG "
                + " where LAGER_I_ID=? and ARTIKEL_I_ID=? and B_ABGANG=1 and B_HISTORIE=0 "
                + "group by LAGER_I_ID, CAST(B.T_BELEGDATUM AS CHAR(10)), B.T_BUCHUNGSZEIT, B.I_ID, B_ABGANG, N_EINSTANDSPREIS, U.N_GESTEHUNGSPREIS "
                + "order by LAGER_I_ID, T_BELEGDATUM, T_BUCHUNGSZEIT, B_ABGANG";
    }
    org.hibernate.Query query = session.createSQLQuery(sQuery);
    query.setParameter(0, lagerIId);
    query.setParameter(1, artikelIId);
    query.setParameter(2, lagerIId);
    query.setParameter(3, artikelIId);
    List<?> list = query.list();
    ArtikelGestehungspreisCalc agc = new ArtikelGestehungspreisCalc(list);
    int lageranzahl = agc.getLageranzahl();
    agc.doRecalc();
    if (debugFile) {
        agc.saveUpdateSQL("c:/temp/gp" + artikelIId + ".sql");
        agc.save2Csv("c:/temp/gp" + artikelIId + ".csv");
        agc.saveInfo("c:/temp/gp" + artikelIId + ".info");
    }
}