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.mg.framework.support.report.ReportUtils.java

License:Open Source License

/**
 *   BIRT    BAi, ..  BIRT   , //w  ww .  ja v a 2  s .c  o  m
 * ? ?? ??  ? ?      ,
 *    ?     ? custom  ??. ? 
 *  ??  Entity.  ??  ? ? 
 * {@link com.mg.framework.api.report.RptProperties#ENTITY_IDS_DATASET_PARAMETER}  {@link
 * com.mg.framework.api.report.RptProperties#BUSINESS_SERVICE_DATASET_PARAMETER}
 *
 * @param name          ? 
 * @param value            
 * @param entityManager   ??
 * @return   
 */
public static Object convertParamValue(String name, Object value, Object entityManager) {
    if (value instanceof String) {
        String strValue = (String) value;
        if (RptProperties.ENTITY_IDS_DATASET_PARAMETER.equalsIgnoreCase(name)) {
            //{<java type>#<identificator value>}[,{<java type>#<identificator value>}]
            if (strValue.startsWith("{") && strValue.endsWith("}")) {
                String[] strIds = StringUtils.split(strValue.substring(1, strValue.length() - 1), "},{")
                        .toArray(new String[0]);
                int length = strIds.length;
                if (length == 0)
                    return value;
                // ?? ?   > 1,  ? ? 
                //   ? ??? ?   QL ?
                if (length > 1) {
                    Serializable[] ids = new Serializable[strIds.length];
                    for (int i = 0; i < strIds.length; i++)
                        ids[i] = convertStringToID(strIds[i]);
                    return ids;
                } else
                    return convertStringToID(strIds[0]);
            } else
                return value;
        } else if (RptProperties.BUSINESS_SERVICE_DATASET_PARAMETER.equalsIgnoreCase(name)) {
            //? ?? ?-
            return ApplicationDictionaryLocator.locate().getBusinessService(strValue);
        } else if (strValue.startsWith("${entity:") && strValue.endsWith("}")) {
            //? ? ?? ??,   ??
            //${entity:<entity name>#<identificator value>[;searchHelp:<search help name>]}
            if (logger != null)
                logger.debug("convert entity parameter: " + strValue);
            strValue = strValue.substring(9, strValue.length() - 1);
            //   searchHelp
            int idx = strValue.indexOf(";searchHelp:");
            if (idx != -1)
                strValue = strValue.substring(0, idx);
            String[] entityInfo = strValue.split("#");
            //???  ? ?  ??
            if (entityInfo.length != 2) {
                if (logger != null)
                    logger.error("invalid entity description: " + strValue);
                return null;
            }
            Session entityManagerImpl = (Session) entityManager;
            ClassMetadata classMetadata = entityManagerImpl.getSessionFactory().getClassMetadata(entityInfo[0]);
            if (classMetadata == null) {
                logger.error("metadata of entity not found: " + entityInfo[0]);
                return null;
            }
            Type type = classMetadata.getIdentifierType();
            Serializable entityID = entityInfo[1];
            if (type instanceof IntegerType)
                entityID = Integer.parseInt((String) entityID);
            if (type instanceof LongType)
                entityID = Long.parseLong((String) entityID);
            return entityManagerImpl.get(entityInfo[0], entityID);
        } else
            return value;
    } else
        return value;
}

From source file:com.mg.jet.birt.report.data.oda.ejbql.HibernateUtil.java

License:Open Source License

public static String[] getHibernateProp(String className) {
    Session session = HibernateUtil.currentSession();
    SessionFactory sf = session.getSessionFactory();
    String[] hibClassProps = sf.getClassMetadata(className).getPropertyNames();

    return (hibClassProps);
}

From source file:com.mg.jet.birt.report.data.oda.ejbql.HibernateUtil.java

License:Open Source License

public static String getHibernatePropTypes(String className, String propName) {

    Session session = HibernateUtil.currentSession();
    SessionFactory sf = session.getSessionFactory();
    org.hibernate.type.Type hibClassProps = sf.getClassMetadata(className).getPropertyType(propName);
    return (hibClassProps.getName());

}

From source file:com.mg.jet.birt.report.data.oda.ejbql.HibernateUtil.java

License:Open Source License

public static Object getHibernatePropVal(Object instObj, String className, String propName) {

    Session session = HibernateUtil.currentSession();
    SessionFactory sf = session.getSessionFactory();
    Object hibObj = sf.getClassMetadata(className).getPropertyValue(instObj, propName, EntityMode.POJO);
    return (hibObj);

}

From source file:com.mtech.easyexchange.dao.impl.AbstractDaoImpl.java

License:Open Source License

protected AbstractDaoImpl(Class<T> entityClass, Session session) {
    this.entityClass = entityClass;
    this.sessionFactory = session.getSessionFactory();
}

From source file:com.myapp.core.base.dao.impl.AbstractBaseDao.java

public Serializable addNewEntity(Object entity) throws SaveException {
    if (entity != null) {
        checkLicense(entity);//w  ww.j a v  a2 s  .c  o  m
        Session session = getCurrentSession();
        if (!(entity instanceof SubsystemTreeInfo)) {
            Class claz = entity.getClass();
            String entityClaz = claz.getName();
            String hql = "select id from SubsystemTreeInfo where entityClaz=?";
            List datas = findByHQL(hql, new String[] { entityClaz });
            if (datas == null || datas.size() <= 0) {
                SubsystemTreeInfo subTree = new SubsystemTreeInfo();
                AbstractEntityPersister cmd = (AbstractEntityPersister) session.getSessionFactory()
                        .getClassMetadata(claz);
                subTree.setEntityClaz(entityClaz);
                String tableName = cmd.getTableName();
                subTree.setEntityTable(tableName);
                long seq = UuidUtils.getStringLong(entityClaz);
                subTree.setEntityObjectType(UuidUtils.getEntityType(entityClaz));
                subTree.setEntitySeq(new Date().getTime());
                subTree.setSeq(seq);
                subTree.setEntityType(getEntityType(entity));
                String entityName = claz.getSimpleName();
                MyEntityAnn myA = (MyEntityAnn) claz.getAnnotation(MyEntityAnn.class);
                if (myA != null) {
                    entityName = myA.name();
                }
                subTree.setEntityName(entityName);
                session.save(subTree);
            }
        }
        if (entity instanceof CoreBaseTreeInfo) {
            CoreBaseTreeInfo treeInfo = (CoreBaseTreeInfo) entity;
            Integer level = 1;
            String longNumber = treeInfo.getNumber();
            String displayName = treeInfo.getName();
            Boolean isLeaf = Boolean.TRUE;
            if (treeInfo.getParent() != null) {
                CoreBaseTreeInfo ptreeInfo = (CoreBaseTreeInfo) treeInfo.getParent();
                longNumber = ptreeInfo.getLongNumber();
                if (!BaseUtil.isEmpty(longNumber)) {
                    longNumber = longNumber + "!" + treeInfo.getNumber();
                } else {
                    longNumber = treeInfo.getNumber();
                }
                displayName = ptreeInfo.getDisplayName();
                if (!BaseUtil.isEmpty(displayName)) {
                    displayName = displayName + "_" + treeInfo.getName();
                } else {
                    displayName = treeInfo.getName();
                }
                level = ptreeInfo.getLevel();
                if (level != null) {
                    level = level + 1;
                } else {
                    level = 1;
                }
            }
            treeInfo.setIsLeaf(isLeaf);
            treeInfo.setLevel(level);
            treeInfo.setLongNumber(longNumber);
            treeInfo.setDisplayName(displayName);
        }
        if (entity instanceof CoreBaseInfo) {
            CoreBaseInfo cbinfo = (CoreBaseInfo) entity;
            Date curDate = new Date();
            cbinfo.setCreateDate(curDate);
            cbinfo.setLastUpdateDate(curDate);
        }
        Serializable pk = session.save(entity);
        session.merge(entity);
        if (entity instanceof CoreBaseTreeInfo) {
            CoreBaseTreeInfo treeInfo = (CoreBaseTreeInfo) entity;
            Object pObj = treeInfo.getParent();
            if (pObj != null && pObj instanceof CoreBaseTreeInfo) {
                CoreBaseTreeInfo pTreeInfo = (CoreBaseTreeInfo) pObj;
                pTreeInfo.setIsLeaf(false);
                session.merge(pTreeInfo);
            }
        }
        session.flush();
        return pk;
    }
    return null;
}

From source file:com.mycompany.testes.Conectar.java

public static void main(String[] args) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Pessoa pessoa;/*  w ww .  ja  va2  s . co m*/

    session.beginTransaction();
    for (int i = 0; i <= 100; i++) {
        pessoa = new Pessoa();
        UUID variavel = UUID.randomUUID();
        pessoa.setNome(variavel.toString());
        session.save(pessoa);
        System.out.println(pessoa.getNome());
    }
    session.getTransaction().commit();
    session.getSessionFactory().close();
}

From source file:com.mycompany.testes.TesteIncluirUsuario.java

public static void main(String[] args) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Usuario usuario = new Usuario();

    session.beginTransaction();/*from w w  w  . java2 s .  c o m*/

    usuario.setCPF(16177168809l);
    usuario.setDataBase("marcos_vassoler");
    usuario.setDateInclude(new Date());
    usuario.setDialect("org.hibernate.dialect.MySQLDialect");
    usuario.setDriverClass("com.mysql.jdbc.Driver");
    usuario.setEmail("marcosvassoler@yahoo.com.br");
    usuario.setName("Marcos Vassoler");
    usuario.setPasswordUser("mitco");
    usuario.setUserName("mvassoler");
    usuario.setUrl("jdbc:mysql://localhost:3306/");
    session.save(usuario);

    session.getTransaction().commit();
    session.getSessionFactory().close();
}

From source file:com.mycompany.testes.TesteTrocarConfiguracao.java

public static void main(String[] args) {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Usuario usuario = null;/*from w  ww.j a  v  a2  s.c  om*/

    session.beginTransaction();

    Query query = session
            .createQuery("from Usuario u WHERE u.userName = 'mvassoler' and u.passwordUser = 'mitco'");
    usuario = (Usuario) query.uniqueResult();

    session.getSessionFactory().close();

    System.out.println(usuario.getName());

    Configuration config = new Configuration().configure("hibernate.cfgGen.xml");
    config.setProperty("connection.driver_class", usuario.getDriverClass());
    config.setProperty("connection.url", usuario.getUrl() + usuario.getDataBase());
    config.setProperty("hibernate.dialect", usuario.getDialect());
    SchemaExport se = new SchemaExport(config);
    se.create(true, true);
}

From source file:com.netsteadfast.greenstep.util.DataUtils.java

License:Apache License

public static Map<String, ClassMetadata> getClassMetadata(Session session) throws Exception {
    Map<String, ClassMetadata> classMetadataMap = session.getSessionFactory().getAllClassMetadata();
    return classMetadataMap;
}