Example usage for javax.persistence EntityManagerFactory getMetamodel

List of usage examples for javax.persistence EntityManagerFactory getMetamodel

Introduction

In this page you can find the example usage for javax.persistence EntityManagerFactory getMetamodel.

Prototype

public Metamodel getMetamodel();

Source Link

Document

Return an instance of Metamodel interface for access to the metamodel of the persistence unit.

Usage

From source file:com.clustercontrol.accesscontrol.util.ObjectPrivilegeUtil.java

/** ??? */
private static void createObjectPrivilegeMap() {
    if (m_objectPrivilegeMap == null || m_objectPrivilegeMap.size() == 0) {
        EntityManagerFactory emf = new JpaTransactionManager().getEntityManager().getEntityManagerFactory();
        Set<EntityType<?>> entityTypes = emf.getMetamodel().getEntities();
        String str = "";
        for (EntityType<?> entityType : entityTypes) {
            Class<?> clazz = entityType.getBindableJavaType();
            if (ObjectPrivilegeTargetInfo.class.isAssignableFrom(clazz)) {
                try {
                    HinemosObjectPrivilege hinemosObjectPrivilege = clazz
                            .getAnnotation(HinemosObjectPrivilege.class);
                    String objectType = hinemosObjectPrivilege.objectType();
                    if (hinemosObjectPrivilege.isModifyCheck()) {
                        str += "[" + objectType + "," + clazz + "] ";

                        if (m_objectPrivilegeMap.get(objectType) != null) {
                            m_log.warn("duplicate objectType=" + objectType);
                        }/* w  w  w.jav a2  s.  c  o  m*/
                        m_objectPrivilegeMap.put(objectType, clazz);
                    }
                } catch (Exception e) {
                    continue;
                }
            }
        }
        m_log.info("objectMap=" + str);
    }
}

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

public static String getVersionFieldName(EntityManagerFactory emf, Class entityClass) {
    EntityType entityType = emf.getMetamodel().entity(entityClass);
    SingularAttribute version;//from ww  w.  ja va 2  s  .  c o m
    if (entityType != null && (version = entityType.getVersion(Long.class)) != null) {
        return version.getName();
    }
    return null;
}

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 ww  w.  j  ava2s.  c  o m
    return classes;
}

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

public static Set<Attribute> getCollectionDescriptors(EntityManagerFactory emf, Class entityClass) {
    Set<Attribute> attributes = new HashSet<Attribute>();
    EntityType entityType = emf.getMetamodel().entity(entityClass);
    for (Object o : entityType.getDeclaredAttributes()) {
        Attribute attribute = (Attribute) o;
        if (Collection.class.isAssignableFrom(attribute.getJavaType())) {
            attributes.add(attribute);//  w ww . j a  va 2 s.co  m
        }
    }
    return attributes;
}

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

public static Map<String, Field> getNonCollectionReferences(EntityManagerFactory emf, Class entityClass) {
    Map<String, Field> fields = new HashMap<String, Field>();
    appendNonCollectionReferences(emf.getMetamodel().entity(entityClass).getAttributes(), fields, "");
    return fields;
}

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));
        }/*from  w w w  . j  av  a2  s  .c o 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 www . j a  v a 2  s .  c  om

        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: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   ww  w.ja  v  a 2 s .co  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:org.jnap.core.persistence.factory.DaoFactoryBkp.java

public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
    Assert.isAssignable(DefaultListableBeanFactory.class, registry.getClass(),
            "The DaoFactory only works within a DefaultListableBeanFactory capable"
                    + "BeanFactory, your BeanDefinitionRegistry is " + registry.getClass());
    final DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) registry;

    // Initialize all SessionFactory beans
    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  a2 s. c om

    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);
                beanFactory.registerBeanDefinition(daoName, createDaoDefinition(entityClass, factory));
                daoNameCache.put(entityClass, daoName);
            }
        }
    }

    factories.clear();
    factories = null;
}

From source file:com.hgsoft.example.hello.web.HelloServlet.java

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    System.out.println("doPost");
    WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    //      String[] beanNames = context.getBeanDefinitionNames();
    //      if(beanNames != null)
    //      {//from   ww  w.  j  av  a  2  s.  c  o  m
    //         for(String beanName:beanNames)
    //         {
    //            System.out.println("bean name >> "+beanName);
    //         }
    //      }
    //      HelloWorldBusiness helloWorldBusiness = context.getBean("helloWorldBusiness",HelloWorldBusiness.class);
    //      helloWorldBusiness.hello();

    //       BlueprintContainer container = (BlueprintContainer) getServletContext().getAttribute(BlueprintContextListener.CONTAINER_ATTRIBUTE);
    //
    //       System.out.println("BlueprintContainer "+container);
    //       
    //       Set<String> componentIds =  container.getComponentIds();
    //       if(componentIds != null)
    //      {
    //         for(String componentId:componentIds)
    //         {
    //            System.out.println("componentId >> "+componentId);
    //         }
    //      }
    //jndi?
    HelloWorldService helloWorldService = (HelloWorldService) JNDIHelper.getHelloWorldService();
    helloWorldService.hello();

    EntityManagerFactory emf = (EntityManagerFactory) context.getBean("entityManagerFactory");
    Metamodel model = emf.getMetamodel();

    resp.setContentType("text/html");
    PrintWriter out = resp.getWriter();
    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    out.println("<HTML>");
    out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
    out.println("  <BODY>");
    out.print("    This is the result of test: ");
    if (null != model) {
        if (null != model.getEntities()) {
            for (EntityType type : model.getEntities()) {
                out.print(type.getName());
            }
        }

    }
    out.println("  </BODY>");
    out.println("</HTML>");
    out.flush();
    out.close();

}