Example usage for javax.persistence.metamodel EntityType getName

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

Introduction

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

Prototype

String getName();

Source Link

Document

Return the entity name.

Usage

From source file:de.micromata.genome.jpa.metainf.MetaInfoUtils.java

/**
 * To entity meta data.//from w  w w .j av a 2s.  c om
 *
 * @param mt the mt
 * @return the entity metadata
 */
private static EntityMetadata toEntityMetaData(ManagedType<?> mt) {
    EntityMetadataBean ret = new EntityMetadataBean();
    ret.setJavaType(mt.getJavaType());
    if (mt instanceof EntityType) {
        EntityType ift = (EntityType) mt;
        ret.setDatabaseName(ift.getName());
    }
    // TODO RK delete propDesc
    List<PropertyDescriptor> propDescs = Arrays.asList(PropertyUtils.getPropertyDescriptors(ret.getJavaType()));
    Set<?> attr = mt.getAttributes();
    for (Object oa : attr) {

        Attribute<?, ?> at = (Attribute<?, ?>) oa;
        Optional<PropertyDescriptor> pdo = propDescs.stream().filter((el) -> el.getName().equals(at.getName()))
                .findFirst();
        ColumnMetadata colm = getColumnMetaData(ret, mt.getJavaType(), at, pdo);
        if (colm != null) {
            ret.getColumns().put(colm.getName(), colm);
        }
    }
    /// EntityType.getName() is not correct.
    Table[] tabs = mt.getJavaType().getAnnotationsByType(Table.class);
    if (tabs != null && tabs.length > 0) {
        for (Table tab : tabs) {
            if (StringUtils.isNotBlank(tab.name()) == true) {
                ret.setDatabaseName(tab.name());
                break;
            }
        }
    }
    return ret;
}

From source file:cn.aozhi.songify.repository.JpaMappingTest.java

@Test
public void allClassMapping() throws Exception {
    Metamodel model = em.getEntityManagerFactory().getMetamodel();
    assertThat(model.getEntities()).as("No entity mapping found").isNotEmpty();

    for (EntityType entityType : model.getEntities()) {
        String entityName = entityType.getName();
        em.createQuery("select o from " + entityName + " o").getResultList();
        logger.info("ok: " + entityName);
    }//from   w w w.jav  a 2s . c  o  m
}

From source file:$.JpaMappingTest.java

@Test
    public void allClassMapping() throws Exception {
        Metamodel model = em.getEntityManagerFactory().getMetamodel();
        assertThat(model.getEntities()).as("No entity mapping found").isNotEmpty();

        for (EntityType entityType : model.getEntities()) {
            String entityName = entityType.getName();
            em.createQuery("select o from " + entityName + " o").getResultList();
            logger.info("ok: " + entityName);
        }/*from   w w w.  j  a v  a  2s .c  o m*/
    }

From source file:com.xyxy.platform.examples.showcase.repository.jpa.JpaMappingTest.java

@Test
public void allClassMapping() throws Exception {
    Metamodel model = em.getEntityManagerFactory().getMetamodel();

    assertThat(model.getEntities()).as("No entity mapping found").isNotEmpty();

    for (EntityType entityType : model.getEntities()) {
        String entityName = entityType.getName();
        em.createQuery("select o from " + entityName + " o").getResultList();
        logger.info("ok: " + entityName);
    }/*  w  w w .j  a v  a2  s.c  o m*/
}

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)
    //      {/*w w  w .j  a  va  2 s  .co 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();

}

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

@Override
public void define() {

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

    logger.info("Getting the list of JPA mapped classes");
    for (EntityType<?> classMapping : classMappings) {
        logger.debug("     Adding JPA persisted class: " + classMapping.getName());
        defineTypes(classMapping);//w  w w . j ava  2 s .  co m
    }

    // Set the super type
    defineSuperType();

    // Set the base types
    setBaseTypes();

    // Define the properties for the Types 
    // This will end up defining the simple types
    defineProperties();

    postProcess();
}

From source file:org.makersoft.activerecord.jpa.JPQL.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private Set<String> getColumns() {
    Metamodel metamodel = em().getMetamodel();
    Set<String> columns = new HashSet<String>();
    for (EntityType entityType : metamodel.getEntities()) {
        if (entityName.equals(entityType.getName())) {
            Set<Attribute> attributes = entityType.getAttributes();
            for (Attribute attribute : attributes) {
                columns.add(attribute.getName());
            }/*w  w  w. j  a v a 2s  .  co  m*/
        }
    }

    return columns;
}

From source file:com.proteanplatform.protean.ProteanUser.java

public void sendGoBar() {
    GoBar goBar = new GoBar();
    for (EntityType<?> entity : proteanServer.getEntities().values()) {
        if (entityUserSecurity.hasAccess(this, entity)) {
            GoCommand command = new GoCommand();
            command.setLabel(entity.getName());
            command.setValue(entity.getName());
            goBar.getCommands().add(command);
        }/*from w  ww  .  j av a2s . c o m*/
    }

    load(goBar);
}

From source file:com.validation.manager.core.history.VersionableTest.java

@Test
public void testVersioning() {
    try {/*from w  w  w  . j  a  v a  2  s.  c  o  m*/
        DemoBuilder.buildDemoProject();
        EntityManager em = DataBaseManager.getEntityManager();
        //Populate with demo classes.
        for (EntityType<?> entity : em.getMetamodel().getEntities()) {
            if (Versionable.class.isAssignableFrom(entity.getJavaType())) {
                final String className = entity.getName();
                System.out.println("Testing class: " + className);
                //Make sure they have at least one auditable field.
                List<Field> fields = FieldUtils.getFieldsListWithAnnotation(entity.getJavaType(),
                        Auditable.class);
                assertEquals(false, fields.isEmpty());
                //Get one from the demo data
                Versionable v = (Versionable) DataBaseManager
                        .createdQuery("Select a from " + entity.getJavaType().getSimpleName() + " a").get(0);
                assertNotNull(v);
                v.updateHistory();
                int count = v.getHistoryList().size();
                for (Field f : fields) {
                    //Now pick one of the Auditable fields
                    assertEquals(count, v.getHistoryList().size());
                    History history = v.getHistoryList().get(v.getHistoryList().size() - 1);
                    assertEquals(count == 1 ? "audit.general.creation" : "audit.general.modified",
                            history.getReason());
                    assertEquals(0, history.getMajorVersion());
                    assertEquals(0, history.getMidVersion());
                    assertEquals(count++, history.getMinorVersion());
                    assertEquals(1, (int) history.getModifierId().getId());
                    assertNotNull(history.getModificationTime());
                    assertTrue(checkHistory(v));
                    assertFalse(Versionable.auditable(v));
                    System.out.println(
                            "Changing field: " + f.getName() + " Type: " + f.getType().getSimpleName());
                    f.setAccessible(true);
                    if (f.getType() == Integer.class) {
                        Integer current = (Integer) f.get(v);
                        Integer newValue = current + 1;
                        showChange(current, newValue);
                        f.set(v, newValue);
                    } else if (f.getType() == String.class) {
                        String current = (String) f.get(v);
                        String newValue = current + 1;
                        showChange(current, newValue);
                        f.set(v, newValue);
                    } else if (f.getType() == byte[].class) {
                        byte[] current = (byte[]) f.get(v);
                        byte[] append = "1".getBytes();
                        byte[] newValue = new byte[current.length + append.length];
                        showChange(current, newValue);
                        f.set(v, newValue);
                    } else if (f.getType() == Boolean.class) {
                        Boolean current = (Boolean) f.get(v);
                        Boolean newValue = !current;
                        showChange(current, newValue);
                        f.set(v, newValue);
                    } else {
                        fail("Unexpected field type: " + f.getType().getSimpleName());
                    }
                    assertTrue(Versionable.auditable(v));
                    v.updateHistory();
                    assertEquals(count, v.getHistoryList().size());
                    history = v.getHistoryList().get(v.getHistoryList().size() - 1);
                    assertEquals(0, history.getMajorVersion());
                    assertEquals(0, history.getMidVersion());
                    assertEquals(count, history.getMinorVersion());
                    assertEquals(1, (int) history.getModifierId().getId());
                    assertEquals("audit.general.modified", history.getReason());
                    assertNotNull(history.getModificationTime());
                    assertTrue(checkHistory(v));
                    assertFalse(Versionable.auditable(v));
                    int total = new HistoryJpaController(DataBaseManager.getEntityManagerFactory())
                            .getHistoryCount();
                    //Test for issue #25 https://github.com/javydreamercsw/validation-manager/issues/25
                    v = (Versionable) DataBaseManager.getEntityManager().find(entity.getJavaType(),
                            DataBaseManager.getEntityManagerFactory().getPersistenceUnitUtil()
                                    .getIdentifier(v));
                    assertTrue(checkHistory(v));
                    assertEquals(total, new HistoryJpaController(DataBaseManager.getEntityManagerFactory())
                            .getHistoryCount());
                    assertEquals(count, v.getHistoryList().size());
                }
            }
        }
    } catch (Exception ex) {
        Exceptions.printStackTrace(ex);
        fail();
    }
}

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

/**
 * Null References on one to many and one to one associations.
 * Will only work if association has annotated with a mappedBy attribute.
 * /*from www .jav  a2  s  .  c o m*/
 * @param entity entity
 */
private void nullReferences(T entity) {
    EntityType<T> type = em.getMetamodel().entity(getEntityClass());

    if (log.isDebugEnabled())
        log.debug("Null references on entity " + type.getName());

    for (Attribute<?, ?> a : type.getAttributes()) {
        if (PersistentAttributeType.ONE_TO_MANY == a.getPersistentAttributeType()
                || PersistentAttributeType.ONE_TO_ONE == a.getPersistentAttributeType()) {
            Object association = PropertyAccessorFactory.forDirectFieldAccess(entity)
                    .getPropertyValue(a.getName());
            if (association != null) {
                EntityType<?> associationType = null;
                if (a.isCollection()) {
                    associationType = em.getMetamodel()
                            .entity(((PluralAttribute<?, ?, ?>) a).getBindableJavaType());
                } else {
                    associationType = em.getMetamodel().entity(a.getJavaType());

                }

                String mappedBy = JpaUtils.getMappedBy(a);
                if (mappedBy != null) {
                    Attribute<?, ?> aa = associationType.getAttribute(mappedBy);
                    if (PersistentAttributeType.MANY_TO_ONE == aa.getPersistentAttributeType()) {
                        if (log.isDebugEnabled()) {
                            log.debug("Null ManyToOne reference on " + associationType.getName() + "."
                                    + aa.getName());
                        }
                        for (Object o : (Collection<?>) association) {
                            PropertyAccessorFactory.forDirectFieldAccess(o).setPropertyValue(aa.getName(),
                                    null);
                        }
                    } else if (PersistentAttributeType.ONE_TO_ONE == aa.getPersistentAttributeType()) {
                        if (log.isDebugEnabled()) {
                            log.debug("Null OneToOne reference on " + associationType.getName() + "."
                                    + aa.getName());
                        }
                        PropertyAccessorFactory.forDirectFieldAccess(association).setPropertyValue(aa.getName(),
                                null);
                    }
                }
            }
        }
    }
}