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

    factories.clear();
    factories = null;
}

From source file:org.kuali.rice.krad.data.jpa.eclipselink.KradEclipseLinkEntityManagerFactoryBeanTest.java

@Test
public void testFull() throws Exception {
    loadContext(getClass().getSimpleName() + "_Full.xml");

    Set<EntityType<?>> fullEntities = entityManagerFactory.getMetamodel().getEntities();
    assertEquals(5, fullEntities.size());
    Set<Class<?>> entityClasses = new HashSet<Class<?>>();
    for (EntityType<?> entityType : fullEntities) {
        entityClasses.add(entityType.getJavaType());
    }/*from   w  w  w.j a  v a2s . c o m*/

    assertTrue(entityClasses.contains(TestEntity.class));
    assertTrue(entityClasses.contains(TestEntity1.class));
    assertTrue(entityClasses.contains(TestEntity2.class));
    assertTrue(entityClasses.contains(TestEntity3.class));
    assertTrue(entityClasses.contains(TestEntity4.class));

    assertFalse(isJtaEnabled());
}

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

protected void defineTypes(EntityType<?> classMapping) {
    JPAType dataType = new JPAType(classMapping);
    logger.debug("Defined data type: " + dataType.getName());
    addType(classMapping.getJavaType().getName(), dataType);

    for (Type type : dataType.getEmbeddableTypes()) {
        addType(type.getName(), type);/*from w  ww  . j  a  v a 2 s .  c  o m*/
    }
}

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

@Test
public void testVersioning() {
    try {//from   ww w.j a  va 2 s  . c om
        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.apache.openejb.util.proxy.QueryProxy.java

private void remove(final Object[] args, final Class<?> returnType) {
    if (args != null && args.length == 1 && returnType.equals(Void.TYPE)) {
        Object entity = args[0];//from   ww  w . j  av  a 2 s  . c  om
        if (!em.contains(entity)) { // reattach the entity if possible
            final Class<?> entityClass = entity.getClass();
            final EntityType<? extends Object> et = em.getMetamodel().entity(entityClass);

            if (!et.hasSingleIdAttribute()) {
                throw new IllegalArgumentException("Dynamic EJB doesn't manage IdClass yet");
            }

            SingularAttribute<?, ?> id = null; // = et.getId(entityClass); doesn't work with openJPA
            for (final SingularAttribute<?, ?> sa : et.getSingularAttributes()) {
                if (sa.isId()) {
                    id = sa;
                    break;
                }
            }
            if (id == null) {
                throw new IllegalArgumentException("id field not found");
            }
            final String idName = id.getName();

            final Object idValue;
            try {
                idValue = BeanUtils.getProperty(entity, idName);
            } catch (final InvocationTargetException e) {
                throw new IllegalArgumentException("can't invoke to get entity id");
            } catch (final NoSuchMethodException e) {
                throw new IllegalArgumentException("can't find the method to get entity id");
            } catch (final IllegalAccessException e) {
                throw new IllegalArgumentException("can't access field/method to get entity id");
            }

            entity = em.getReference(et.getJavaType(), idValue);
            if (entity == null) {
                throw new IllegalArgumentException("entity " + entity + " is not managed and can't be found.");
            }
        }
        em.remove(entity);
    } else {
        throw new IllegalArgumentException(REMOVE_NAME + " should have only one parameter and return void");
    }
}