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:kr.debop4j.data.hibernate.tools.StatelessTool.java

License:Apache License

/**
 * Open stateless session./*from w  w  w . ja  v a2  s .  com*/
 *
 * @param session the session
 * @return the stateless session
 */
public static StatelessSession openStatelessSession(Session session) {
    Guard.shouldNotBeNull(session, "session");
    return session.getSessionFactory().openStatelessSession();
}

From source file:lucee.runtime.orm.hibernate.HibernateORMSession.java

License:Open Source License

SessionFactory getSessionFactory(Key datasSourceName) throws PageException {
    Session s = getSession(datasSourceName);
    return s.getSessionFactory();
}

From source file:lucee.runtime.orm.hibernate.HibernateORMSession.java

License:Open Source License

@Override
public Component load(PageContext pc, String cfcName, String id) throws PageException {
    //Component cfc = create(pc,cfcName);

    Component cfc = data.getEngine().create(pc, this, cfcName, false);
    Key dsn = KeyImpl.init(ORMUtil.getDataSourceName(pc, cfc));
    Session sess = getSession(dsn);
    String name = HibernateCaster.getEntityName(cfc);
    Object obj = null;/* w  w w .  j  a va 2s  .c  o m*/
    try {
        ClassMetadata metaData = sess.getSessionFactory().getClassMetadata(name);
        if (metaData == null)
            throw ExceptionUtil.createException(this, null,
                    "could not load meta information for entity [" + name + "]", null);
        Serializable oId = CommonUtil
                .toSerializable(CommonUtil.castTo(pc, metaData.getIdentifierType().getReturnedClass(), id));
        obj = sess.get(name, oId);
    } catch (Throwable t) {
        throw CommonUtil.toPageException(t);
    }

    return (Component) obj;
}

From source file:lucee.runtime.orm.hibernate.HibernateORMSession.java

License:Open Source License

private Object loadByExample(PageContext pc, Object obj, boolean unique) throws PageException {
    Component cfc = HibernateCaster.toComponent(obj);
    Key dsn = KeyImpl.init(ORMUtil.getDataSourceName(pc, cfc));
    ComponentScope scope = cfc.getComponentScope();
    String name = HibernateCaster.getEntityName(cfc);
    Session sess = getSession(dsn);
    Object rtn = null;//w  ww  . j  av a 2  s.c o m

    try {
        //trans.begin();

        ClassMetadata metaData = sess.getSessionFactory().getClassMetadata(name);
        String idName = metaData.getIdentifierPropertyName();
        Type idType = metaData.getIdentifierType();

        Criteria criteria = sess.createCriteria(name);
        if (!Util.isEmpty(idName)) {
            Object idValue = scope.get(CommonUtil.createKey(idName), null);
            if (idValue != null) {
                criteria.add(Restrictions.eq(idName, HibernateCaster.toSQL(idType, idValue, null)));
            }
        }
        criteria.add(Example.create(cfc));

        // execute

        if (!unique) {
            rtn = criteria.list();
        } else {
            //Map map=(Map) criteria.uniqueResult();
            rtn = criteria.uniqueResult();
        }
    } catch (Throwable t) {
        // trans.rollback();
        throw CommonUtil.toPageException(t);
    }
    //trans.commit();

    return rtn;
}

From source file:lucee.runtime.orm.hibernate.HibernateORMSession.java

License:Open Source License

private Object load(PageContext pc, String cfcName, Struct filter, Struct options, String order, boolean unique)
        throws PageException {
    Component cfc = data.getEngine().create(pc, this, cfcName, false);
    Key dsn = KeyImpl.init(ORMUtil.getDataSourceName(pc, cfc));
    Session sess = getSession(dsn);

    String name = HibernateCaster.getEntityName(cfc);
    ClassMetadata metaData = null;/*from  w  w  w.  j a  v a 2  s.c om*/

    Object rtn;
    try {
        //trans.begin();

        Criteria criteria = sess.createCriteria(name);

        // filter
        if (filter != null && !filter.isEmpty()) {
            metaData = sess.getSessionFactory().getClassMetadata(name);
            Object value;
            Entry<Key, Object> entry;
            Iterator<Entry<Key, Object>> it = filter.entryIterator();
            String colName;
            while (it.hasNext()) {
                entry = it.next();
                colName = HibernateUtil.validateColumnName(metaData, CommonUtil.toString(entry.getKey()));
                Type type = HibernateUtil.getPropertyType(metaData, colName, null);
                value = entry.getValue();
                if (!(value instanceof Component))
                    value = HibernateCaster.toSQL(type, value, null);

                if (value != null)
                    criteria.add(Restrictions.eq(colName, value));
                else
                    criteria.add(Restrictions.isNull(colName));
            }
        }

        // options
        boolean ignoreCase = false;
        if (options != null && !options.isEmpty()) {
            // ignorecase
            Boolean ignorecase = CommonUtil.toBoolean(options.get("ignorecase", null), null);
            if (ignorecase != null)
                ignoreCase = ignorecase.booleanValue();

            // offset
            int offset = CommonUtil.toIntValue(options.get("offset", null), 0);
            if (offset > 0)
                criteria.setFirstResult(offset);

            // maxResults
            int max = CommonUtil.toIntValue(options.get("maxresults", null), -1);
            if (max > -1)
                criteria.setMaxResults(max);

            // cacheable
            Boolean cacheable = CommonUtil.toBoolean(options.get("cacheable", null), null);
            if (cacheable != null)
                criteria.setCacheable(cacheable.booleanValue());

            // MUST cacheName ?

            // maxResults
            int timeout = CommonUtil.toIntValue(options.get("timeout", null), -1);
            if (timeout > -1)
                criteria.setTimeout(timeout);
        }

        // order 
        if (!Util.isEmpty(order)) {
            if (metaData == null)
                metaData = sess.getSessionFactory().getClassMetadata(name);

            String[] arr = CommonUtil.toStringArray(order, ',');
            CommonUtil.trimItems(arr);
            String[] parts;
            String col;
            boolean isDesc;
            Order _order;
            //ColumnInfo ci;
            for (int i = 0; i < arr.length; i++) {
                parts = CommonUtil.toStringArray(arr[i], " \t\n\b\r");
                CommonUtil.trimItems(parts);
                col = parts[0];

                col = HibernateUtil.validateColumnName(metaData, col);
                isDesc = false;
                if (parts.length > 1) {
                    if (parts[1].equalsIgnoreCase("desc"))
                        isDesc = true;
                    else if (!parts[1].equalsIgnoreCase("asc")) {
                        throw ExceptionUtil.createException((ORMSession) null, null,
                                "invalid order direction defintion [" + parts[1] + "]",
                                "valid values are [asc, desc]");
                    }

                }
                _order = isDesc ? Order.desc(col) : Order.asc(col);
                if (ignoreCase)
                    _order.ignoreCase();

                criteria.addOrder(_order);

            }
        }

        // execute
        if (!unique) {
            rtn = HibernateCaster.toCFML(criteria.list());
        } else {
            rtn = HibernateCaster.toCFML(criteria.uniqueResult());
        }

    } catch (Throwable t) {
        throw CommonUtil.toPageException(t);
    }

    return rtn;
}

From source file:models.papmon.HibernateStat.java

License:Open Source License

public HibernateStat(Date refDate) {
    created = refDate;// w  w  w  . j av  a  2  s  . c o m
    Session session = (Session) JPA.em().getDelegate();
    Statistics stats = session.getSessionFactory().getStatistics();
    queryExecutionCount = stats.getQueryExecutionCount();
    queryExecutionMaxTime = stats.getQueryExecutionMaxTime();
    sessionOpenCount = stats.getSessionOpenCount();
    sessionCloseCount = stats.getSessionCloseCount();
    entityLoadCount = stats.getEntityLoadCount();
    entityInsertCount = stats.getEntityInsertCount();
    entityUpdateCount = stats.getEntityUpdateCount();
    entityDeleteCount = stats.getEntityDeleteCount();
    entityFetchCount = stats.getEntityFetchCount();
    queryCacheHitCount = stats.getQueryCacheHitCount();
    queryCacheMissCount = stats.getQueryCacheMissCount();
    queryCachePutCount = stats.getQueryCachePutCount();
    secondLevelCacheHitCount = stats.getSecondLevelCacheHitCount();
    secondLevelCacheMissCount = stats.getSecondLevelCacheMissCount();
    secondLevelCachePutCount = stats.getSecondLevelCachePutCount();
    stats.clear();
}

From source file:net.sf.beanlib.hibernate3.Hibernate3SequenceGenerator.java

License:Apache License

/** Returns the identifier generator created for the specified sequence and session. */
private static IdentifierGenerator createIdentifierGenerator(String sequenceName, Session session) {
    SessionFactory sessionFactory = session.getSessionFactory();

    if (!(sessionFactory instanceof SessionFactoryImpl))
        throw new IllegalStateException("Not yet know how to handle the session factory of the given session!");
    SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) sessionFactory;
    Settings settings = sessionFactoryImpl.getSettings();
    Dialect dialect = settings.getDialect();

    Properties params = new Properties();
    params.setProperty("sequence", sequenceName);

    return IdentifierGeneratorFactory.create("sequence", TypeFactory.heuristicType("long"), params, dialect);
}

From source file:net.sf.beanlib.hibernate4.Hibernate4SequenceGenerator.java

License:Apache License

/** Returns the identifier generator created for the specified sequence and session. */
private static IdentifierGenerator createIdentifierGenerator(String sequenceName, Session session) {
    SessionFactory sessionFactory = session.getSessionFactory();

    if (!(sessionFactory instanceof SessionFactoryImpl)) {
        throw new IllegalStateException("Not yet know how to handle the session factory of the given session!");
    }/*from ww w.  ja v a  2s  . com*/
    SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) sessionFactory;
    //        Dialect dialect = sessionFactoryImpl.getDialect();
    ServiceRegistry registry = sessionFactoryImpl.getServiceRegistry();

    Properties params = new Properties();
    params.setProperty("sequence", sequenceName);

    SequenceStyleGenerator sequenceStyleGenerator = new SequenceStyleGenerator();
    sequenceStyleGenerator.configure(LongType.INSTANCE, params, (ServiceRegistry) registry);

    //        SequenceGenerator sequenceGenerator = new SequenceGenerator();
    //        sequenceGenerator.configure(LongType.INSTANCE, params, dialect);

    return sequenceStyleGenerator;
}

From source file:nl.intercommit.weaves.grid.HibernatePagedGridDataSource.java

License:Open Source License

public HibernatePagedGridDataSource(final Session session, final Class<T> entityType) {
    super(entityType);
    hibernateSession = session;//from  ww  w.  j  a v a 2 s  .  com
    if (session.getSessionFactory().getClassMetadata(entityType) == null) {
        throw new TapestryException("This entity [" + entityType + "] is not managed by the given session",
                this, new EntityExistsException());
    }
}

From source file:no.abmu.common.persistence.hibernate3.AbstractBaseDaoImpl.java

License:Open Source License

public void saveOrUpdateNewSession(Object obj) {
    Session session = SessionFactoryUtils.getSession(getSessionFactory(), true);
    Session newSession = null;//from  www  . j ava2 s.c  om
    try {
        newSession = session.getSessionFactory().openSession(session.connection());
        newSession.saveOrUpdate(obj);
    } catch (HibernateException ex) {
        throw SessionFactoryUtils.convertHibernateAccessException(ex);
    } finally {
        if (newSession != null) {
            try {
                newSession.flush();
                newSession.close();
            } catch (HibernateException e) {
                throw SessionFactoryUtils.convertHibernateAccessException(e);
            }
        }
    }
}