Example usage for javax.persistence.metamodel EntityType getJavaType

List of usage examples for javax.persistence.metamodel EntityType getJavaType

Introduction

In this page you can find the example usage for javax.persistence.metamodel EntityType getJavaType.

Prototype

Class<X> getJavaType();

Source Link

Document

Return the represented Java type.

Usage

From source file:com.github.gekoh.yagen.util.MappingUtils.java

public static Class[] getEntityClasses(EntityManagerFactory emf) throws Exception {
    Set<EntityType<?>> entities = emf.getMetamodel().getEntities();
    Class[] classes = new Class[entities.size()];
    int arrIdx = 0;
    for (EntityType<?> entityType : entities) {
        classes[arrIdx++] = entityType.getJavaType();
    }/*from   w ww .j  a  v a2s.com*/
    return classes;
}

From source file:com.github.gekoh.yagen.util.MappingUtils.java

public static Map<Class, Set<Class>> getMasterToDetailClassesMap(EntityManagerFactory emf) throws Exception {
    Map<Class, TreeEntity> treeEntities = new HashMap<Class, TreeEntity>();

    for (EntityType entityType : emf.getMetamodel().getEntities()) {
        Class entityClass = entityType.getJavaType();
        if (!treeEntities.containsKey(entityClass)) {
            treeEntities.put(entityClass, new TreeEntity(entityClass));
        }/* w  ww  .  j a  v  a 2s. co m*/

        fillTreeEntityMap(treeEntities, entityType.getAttributes(), entityClass);
    }

    Map<Class, Set<Class>> m2dMap = new HashMap<Class, Set<Class>>();

    for (Map.Entry<Class, TreeEntity> entry : treeEntities.entrySet()) {
        Class detail = entry.getKey();
        for (Class master : entry.getValue().getMasterEntities()) {
            Set<Class> details = m2dMap.get(master);
            if (details == null) {
                m2dMap.put(master, details = new HashSet<Class>());
            }
            details.add(detail);
        }
    }

    return m2dMap;
}

From source file:com.github.gekoh.yagen.util.MappingUtils.java

public static Iterator<Class> getClassesSequenceIterator(EntityManagerFactory emf, boolean topDown)
        throws Exception {
    Map<Class, TreeEntity> treeEntities = new HashMap<Class, TreeEntity>();

    for (EntityType entityType : emf.getMetamodel().getEntities()) {
        Class entityClass = entityType.getJavaType();
        if (!treeEntities.containsKey(entityClass)) {
            treeEntities.put(entityClass, new TreeEntity(entityClass));
        }/*from  w ww  .j a  v  a 2 s  .  c  o  m*/

        fillTreeEntityMap(treeEntities, entityType.getAttributes(), entityClass);
    }

    for (TreeEntity treeEntity : treeEntities.values()) {
        if (topDown) {
            treeEntity.calculateMaxLevel(treeEntities);
        } else {
            treeEntity.calculateMinLevel(treeEntities);
        }
    }

    List<TreeEntity> sorted = new ArrayList<TreeEntity>(treeEntities.values());
    Collections.sort(sorted);

    if (!topDown) {
        Collections.reverse(sorted);
    }

    List<Class> sortedClasses = new ArrayList<Class>(sorted.size());
    for (TreeEntity treeEntity : sorted) {
        sortedClasses.add(treeEntity.getEntityClass());
    }

    return sortedClasses.iterator();
}

From source file:com.sdl.odata.datasource.jpa.JPADatasourceProvider.java

/**
 * Check if the given JPA entity class is a valid entity type.
 *
 * @param jpaEntityClass The given JPA entity class.
 * @return {@code true} if it is valid, {@code false} otherwise.
 *///  ww  w.  j  ava  2 s  .  co m
private boolean isValidEntityType(String jpaEntityClass) {

    Set<EntityType<?>> entityTypes = entityManagerFactory.getMetamodel().getEntities();
    // Check if there is one JPA entity type for which the JPA entity class is the same
    for (EntityType entityType : entityTypes) {
        if (entityType.getJavaType().getName().equals(jpaEntityClass)) {
            return true;
        }
    }
    return false;
}

From source file:com.sdl.odata.datasource.jpa.JPAEdmModelLoader.java

private List<Class<?>> discoverEntities() {
    Map<String, Class<?>> foundEntities = new HashMap<>();

    Metamodel metamodel = entityManagerFactory.getMetamodel();
    for (EntityType t : metamodel.getEntities()) {
        LOG.debug("We have a JPA Entity type: {}", t.getBindableJavaType().getCanonicalName());

        Class<?> entityType = t.getJavaType();

        foundEntities.put(entityType.getName(), entityType);
    }/*from  ww w .  jav  a  2 s.c  o  m*/

    return new ArrayList<>(foundEntities.values());
}

From source file:com.invariantproperties.sandbox.springentitylistener.listener.SpringEntityListenersConfigurer.java

@PostConstruct
public void registerListeners() {
    // get registry so we can add listeners.
    HibernateEntityManagerFactory hemf = (HibernateEntityManagerFactory) entityManagerFactory;
    SessionFactory sf = hemf.getSessionFactory();
    EventListenerRegistry registry = ((SessionFactoryImpl) sf).getServiceRegistry()
            .getService(EventListenerRegistry.class);

    final Set<Object> listeners = new HashSet<Object>();

    EntityManager entityManager = null;/*from ww  w . jav a 2 s .c o  m*/
    try {
        entityManager = hemf.createEntityManager();
        // for every entity known to the system...
        for (EntityType<?> entity : entityManager.getMetamodel().getEntities()) {

            // ... register event listeners for it.
            if (entity.getJavaType().isAnnotationPresent(SpringEntityListeners.class)) {
                SpringEntityListeners annotation = (SpringEntityListeners) entity.getJavaType()
                        .getAnnotation(SpringEntityListeners.class);
                for (Class<?> beanClass : annotation.value()) {
                    Map<String, ?> map = context.getBeansOfType(beanClass);
                    listeners.addAll(map.values());
                }
            }
        }
    } finally {
        if (entityManager != null) {
            entityManager.close();
        }
    }

    // register adapter and listeners.
    HibernateEntityListenersAdapter adapter = new HibernateEntityListenersAdapter(
            new ArrayList<Object>(listeners), entityManagerFactory);
    registry.getEventListenerGroup(EventType.PRE_INSERT).appendListener(adapter);
    registry.getEventListenerGroup(EventType.POST_COMMIT_INSERT).appendListener(adapter);
    registry.getEventListenerGroup(EventType.PRE_UPDATE).appendListener(adapter);
    registry.getEventListenerGroup(EventType.POST_COMMIT_UPDATE).appendListener(adapter);
    registry.getEventListenerGroup(EventType.PRE_DELETE).appendListener(adapter);
    registry.getEventListenerGroup(EventType.POST_COMMIT_DELETE).appendListener(adapter);
    registry.getEventListenerGroup(EventType.POST_LOAD).appendListener(adapter);
}

From source file:org.lightadmin.core.config.bootstrap.LightAdminBeanDefinitionRegistryPostProcessor.java

private Iterable<Class<?>> managedEntities(EntityManager entityManager) {
    Set<Class<?>> managedEntities = newHashSet();
    for (EntityType<?> entity : entityManager.getMetamodel().getEntities()) {
        if (entity.getJavaType() != null)
            managedEntities.add(entity.getJavaType());
    }/*from  w  ww  .  j a  v a 2s. c om*/
    return managedEntities;
}

From source file:tools.xor.service.DatastoreDAS.java

@Override
public List<String> getAggregateList() {
    List<String> result = new ArrayList<String>();

    Metamodel metaModel = emf.getMetamodel();
    Set<EntityType<?>> classMappings = metaModel.getEntities();

    for (EntityType<?> classMapping : classMappings) {
        defineTypes(classMapping);/*www  .ja  v a2  s .c  o m*/
        result.add(classMapping.getJavaType().getName());
    }

    return result;
}

From source file:org.jnap.core.persistence.factory.DaoFactory.java

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    Assert.isAssignable(DefaultListableBeanFactory.class, beanFactory.getClass(),
            "The DaoFactory only works within a DefaultListableBeanFactory capable "
                    + "BeanFactory. Your BeanFactory is " + beanFactory.getClass());
    this.beanFactory = beanFactory;
    final DefaultListableBeanFactory listableBeanFactory = (DefaultListableBeanFactory) beanFactory;

    String[] factoryNames = beanFactory.getBeanNamesForType(EntityManagerFactory.class);
    Set<EntityManagerFactory> factories = new HashSet<EntityManagerFactory>(factoryNames.length);
    for (String factoryName : factoryNames) {
        factories.add(beanFactory.getBean(factoryName, EntityManagerFactory.class));
    }//from w  w  w . j  a  v  a  2 s  .  c o  m

    for (EntityManagerFactory factory : factories) {
        factory.getMetamodel().getEntities();
        for (EntityType<?> entityMetadata : factory.getMetamodel().getEntities()) {
            Class<? extends PersistentModel> entityClass = (Class<? extends PersistentModel>) entityMetadata
                    .getJavaType();
            if (entityClass != null && !isDaoDefinedForEntity(beanFactory, entityClass)) {
                String daoName = buildDaoName(entityClass);
                listableBeanFactory.registerBeanDefinition(daoName, createDaoDefinition(entityClass, factory));
                daoNameCache.put(entityClass, daoName);
            }
        }
    }

    factories.clear();
    factories = null;
}

From source file:utilities.internal.CopyOfDatabaseUtil.java

private Configuration buildConfiguration() {
    Configuration result;/*from ww  w .j  av a  2 s . c  o m*/
    Metamodel metamodel;
    Collection<EntityType<?>> entities;
    Collection<EmbeddableType<?>> embeddables;

    result = new Configuration();
    metamodel = entityManagerFactory.getMetamodel();

    entities = metamodel.getEntities();
    for (EntityType<?> entity : entities)
        result.addAnnotatedClass(entity.getJavaType());

    embeddables = metamodel.getEmbeddables();
    for (EmbeddableType<?> embeddable : embeddables)
        result.addAnnotatedClass(embeddable.getJavaType());

    return result;
}