Example usage for javax.persistence EntityManager getEntityManagerFactory

List of usage examples for javax.persistence EntityManager getEntityManagerFactory

Introduction

In this page you can find the example usage for javax.persistence EntityManager getEntityManagerFactory.

Prototype

public EntityManagerFactory getEntityManagerFactory();

Source Link

Document

Return the entity manager factory for the entity manager.

Usage

From source file:cn.guoyukun.spring.jpa.repository.hibernate.HibernateUtils.java

/**
 * ? jpa EntityManager ?hibernate Cache API
 *
 * @param em//from   www  .jav  a  2  s .co m
 * @return
 * @see #getCache(javax.persistence.EntityManagerFactory)
 */
public static Cache getCache(EntityManager em) {
    return getCache(em.getEntityManagerFactory());
}

From source file:cn.guoyukun.spring.jpa.repository.hibernate.HibernateUtils.java

/**
 * ?jpa EntityManager /*from   www .  ja v  a2 s  .  c  o  m*/
 *
 * @param em
 * @see #evictLevel2Cache(javax.persistence.EntityManagerFactory)
 */
public static void evictLevel2Cache(EntityManager em) {
    evictLevel2Cache(em.getEntityManagerFactory());
}

From source file:cn.guoyukun.spring.jpa.repository.hibernate.HibernateUtils.java

/**
 * ?jpa EntityManager ? hibernate SessionFactory API
 *
 * @param em//from www . j a v  a2  s .c o m
 * @return
 * @see #getSessionFactory(javax.persistence.EntityManagerFactory)
 */
public static SessionFactory getSessionFactory(EntityManager em) {
    return getSessionFactory(em.getEntityManagerFactory());
}

From source file:com.impetus.kundera.rest.common.EntityUtils.java

/**
 * @param entityClassName//from ww  w .j  ava2 s  . c  o m
 * @param em
 * @return
 */
public static Class<?> getEntityClass(String entityClassName, EntityManager em) {
    MetamodelImpl metamodel = (MetamodelImpl) em.getEntityManagerFactory().getMetamodel();
    Class<?> entityClass = metamodel.getEntityClass(entityClassName);
    return entityClass;
}

From source file:com.impetus.kundera.rest.common.EntityUtils.java

/**
 * @param entityClassName/* w  ww .  j av a 2 s.c om*/
 * @param em
 * @return
 */
public static EntityMetadata getEntityMetaData(String entityClassName, EntityManager em) {
    MetamodelImpl metamodel = (MetamodelImpl) em.getEntityManagerFactory().getMetamodel();
    Class<?> entityClass = metamodel.getEntityClass(entityClassName);

    return metamodel.getEntityMetadata(entityClass);
}

From source file:com.impetus.kundera.rest.common.EntityUtils.java

/**
 * @param entityClassName/*from   w  w  w  .  ja va2s.c o  m*/
 * @param em
 * @return
 */
public static AbstractManagedType getEntityManagedType(String entityClassName, EntityManager em) {
    MetamodelImpl metamodel = (MetamodelImpl) em.getEntityManagerFactory().getMetamodel();
    Class<?> entityClass = metamodel.getEntityClass(entityClassName);
    EntityMetadata entityMetadata = metamodel.getEntityMetadata(entityClass);
    AbstractManagedType managedType = (AbstractManagedType) metamodel.entity(entityMetadata.getEntityClazz());
    return managedType;
}

From source file:com.impetus.kundera.rest.common.EntityUtils.java

/**
 * @param queryString//w  w  w  . ja  v a  2s  . c om
 * @param q
 * @param em
 */
public static void setObjectQueryParameters(String queryString, String parameterString, Query q,
        EntityManager em, String mediaType) {
    MetamodelImpl metamodel = (MetamodelImpl) em.getEntityManagerFactory().getMetamodel();

    if (parameterString == null || parameterString.isEmpty()) {
        return;
    }

    Map<String, String> paramsMap = new HashMap<String, String>();
    ObjectMapper mapper = new ObjectMapper();

    try {
        paramsMap = mapper.readValue(parameterString, new TypeReference<HashMap<String, String>>() {
        });
        KunderaQuery kq = ((QueryImpl) q).getKunderaQuery();
        Set<Parameter<?>> parameters = kq.getParameters();
        for (String paramName : paramsMap.keySet()) {
            String value = paramsMap.get(paramName);

            if (paramName.equalsIgnoreCase("firstResult")) {
                q.setFirstResult(Integer.parseInt(value));

            } else if (paramName.equalsIgnoreCase("maxResult")) {
                q.setMaxResults(Integer.parseInt(value));

            } else if (StringUtils.isNumeric(paramName)) {
                for (Parameter param : parameters) {
                    if (param.getPosition() == Integer.parseInt(paramName)) {

                        Class<?> paramClass = param.getParameterType();
                        Object paramValue = null;
                        if (metamodel.isEmbeddable(paramClass)) {
                            paramValue = JAXBUtils.toObject(StreamUtils.toInputStream(value), paramClass,
                                    mediaType);

                        } else {
                            PropertyAccessor accessor = PropertyAccessorFactory.getPropertyAccessor(paramClass);
                            paramValue = accessor.fromString(paramClass, value);
                        }

                        q.setParameter(Integer.parseInt(paramName), paramValue);
                        break;
                    }
                }
            } else {
                for (Parameter param : parameters) {
                    if (param.getName().equals(paramName)) {

                        Class<?> paramClass = param.getParameterType();
                        Object paramValue = null;

                        if (metamodel.isEmbeddable(paramClass)) {
                            paramValue = JAXBUtils.toObject(StreamUtils.toInputStream(value), paramClass,
                                    mediaType);

                        } else {
                            PropertyAccessor accessor = PropertyAccessorFactory.getPropertyAccessor(paramClass);
                            paramValue = accessor.fromString(paramClass, value);
                        }
                        q.setParameter(paramName, paramValue);
                        break;
                    }
                }

            }
        }

    } catch (JsonParseException e) {
        log.error(e.getMessage());
    } catch (JsonMappingException e) {
        log.error(e.getMessage());
    } catch (IOException e) {
        log.error(e.getMessage());
    }
}

From source file:org.jdal.dao.jpa.JpaUtils.java

/** 
 * Initialize entity attribute/*from   w ww  . jav a 2  s .c  o m*/
 * @param em
 * @param entity
 * @param a
 * @param depth
 */
@SuppressWarnings("rawtypes")
private static void intialize(EntityManager em, Object entity, Object attached, Attribute a, int depth) {
    Object value = PropertyAccessorFactory.forDirectFieldAccess(attached).getPropertyValue(a.getName());
    if (!em.getEntityManagerFactory().getPersistenceUnitUtil().isLoaded(value)) {
        em.refresh(value);
    }

    PropertyAccessorFactory.forDirectFieldAccess(entity).setPropertyValue(a.getName(), value);

    initialize(em, value, depth - 1);
}

From source file:org.jdal.dao.jpa.JpaUtils.java

/**
 * Initialize a entity. //from  w  ww . j a va  2  s  .c om
 * @param em entity manager to use
 * @param entity entity to initialize
 * @param depth max depth on recursion
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void initialize(EntityManager em, Object entity, int depth) {
    // return on nulls, depth = 0 or already initialized objects
    if (entity == null || depth == 0) {
        return;
    }

    PersistenceUnitUtil unitUtil = em.getEntityManagerFactory().getPersistenceUnitUtil();
    EntityType entityType = em.getMetamodel().entity(entity.getClass());
    Set<Attribute> attributes = entityType.getDeclaredAttributes();

    Object id = unitUtil.getIdentifier(entity);

    if (id != null) {
        Object attached = em.find(entity.getClass(), unitUtil.getIdentifier(entity));

        for (Attribute a : attributes) {
            if (!unitUtil.isLoaded(entity, a.getName())) {
                if (a.isCollection()) {
                    intializeCollection(em, entity, attached, a, depth);
                } else if (a.isAssociation()) {
                    intialize(em, entity, attached, a, depth);
                }
            }
        }
    }
}

From source file:com.seer.datacruncher.utils.generic.CommonUtils.java

/**
 * Checks whether current persistence unit is SQL SERVER (MS SQL).
 * // www.  java2 s .c  o  m
 * @param em
 * @return
 */
public synchronized static boolean isMsSql(EntityManager em) {
    return em == null ? false
            : em.getEntityManagerFactory() == null ? false
                    : em.getEntityManagerFactory().getProperties()
                            .get("hibernate.connection.driver_class") == null
                                    ? false
                                    : !em.getEntityManagerFactory().getProperties()
                                            .get("hibernate.connection.driver_class")
                                            .equals("net.sourceforge.jtds.jdbc.Driver") ? false : true;
}