javax.persistence.metamodel.Metamodel Java Examples

The following examples show how to use javax.persistence.metamodel.Metamodel. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MCRJPABootstrapper.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
private void checkHibernateMappingConfig(Metamodel metamodel) {
    Set<String> mappedEntities = metamodel
        .getEntities()
        .stream()
        .map(EntityType::getJavaType)
        .map(Class::getName)
        .collect(Collectors.toSet());
    List<String> unMappedEntities = MCRConfiguration2.getString("MCR.Hibernate.Mappings")
        .map(MCRConfiguration2::splitValue)
        .orElseGet(Stream::empty)
        .filter(cName -> !mappedEntities.contains(cName))
        .collect(Collectors.toList());
    if (!unMappedEntities.isEmpty()) {
        throw new MCRException(
            "JPA Mapping is inclomplete. Could not find a mapping for these classes: " + unMappedEntities);
    }
}
 
Example #2
Source File: MCRJPABootstrapper.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void startUp(ServletContext servletContext) {
    try {
        initializeJPA();
    } catch (PersistenceException e) {
        //fix for MCR-1236
        if (MCRConfiguration2.getBoolean("MCR.Persistence.Database.Enable").orElse(true)) {
            LogManager.getLogger()
                .error(() -> "Could not initialize JPA. Database access is disabled in this session.", e);
            MCRConfiguration2.set("MCR.Persistence.Database.Enable", String.valueOf(false));
        }
        MCREntityManagerProvider.init(e);
        return;
    }
    Metamodel metamodel = MCREntityManagerProvider.getEntityManagerFactory().getMetamodel();
    checkHibernateMappingConfig(metamodel);
    LogManager.getLogger()
        .info("Mapping these entities: {}", metamodel.getEntities()
            .stream()
            .map(EntityType::getJavaType)
            .map(Class::getName)
            .collect(Collectors.toList()));
    MCRShutdownHandler.getInstance().addCloseable(new MCRJPAShutdownProcessor());
}
 
Example #3
Source File: JPAModelStructureBuilder.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
private void addRootEntities(ModelStructure modelStructure) {
	Metamodel jpaMetamodel;
	Set<EntityType<?>> jpaEntities;

	String modelName = getDataSource().getConfiguration().getModelName();

	jpaMetamodel = getEntityManager().getMetamodel();
	jpaEntities = jpaMetamodel.getEntities();
	logger.debug("Jpa metamodel contains [" + jpaEntities.size() + "] entity types");

	for (EntityType<?> entityType : jpaEntities) {
		logger.debug("Adding entity type [" + entityType + "] to model structure");
		String entityTypeName = entityType.getJavaType().getName();
		addEntity(modelStructure, modelName, entityTypeName);
		logger.info("Entity type [" + entityType + "] succesfully added to model structure");
	}
}
 
Example #4
Source File: JPAPersistenceManager.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
public EntityType getTargetEntity(RegistryConfiguration registryConf, EntityManager entityManager) {

		EntityType targetEntity;

		String targetEntityName = getTargetEntityName(registryConf);

		Metamodel classMetadata = entityManager.getMetamodel();
		Iterator it = classMetadata.getEntities().iterator();

		targetEntity = null;
		while (it.hasNext()) {
			EntityType entity = (EntityType) it.next();
			String jpaEntityName = entity.getName();

			if (entity != null && jpaEntityName.equals(targetEntityName)) {
				targetEntity = entity;
				break;
			}
		}

		return targetEntity;
	}
 
Example #5
Source File: ODataJPAContextMock.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private static EntityManager mockEntityManager() {
  EntityManager em = EasyMock.createMock(EntityManager.class);
  Metamodel mm = EasyMock.createMock(Metamodel.class);
  EasyMock.expect(em.getMetamodel()).andReturn(mm).anyTimes();
  Set<EntityType<?>> et = new HashSet<EntityType<?>>();
  EasyMock.expect(mm.getEntities()).andReturn(et).anyTimes();
  EasyMock.expect(em.isOpen()).andReturn(true).anyTimes();
  Query jpqlquery = EasyMock.createMock(Query.class);
  Capture<String> capturedArgument = new Capture<String>();
  EasyMock.expect(em.createQuery(EasyMock.capture(capturedArgument))).andReturn(jpqlquery).anyTimes();
  EasyMock.expect(jpqlquery.setParameter(EasyMock.anyInt(), EasyMock.anyObject()))
      .andReturn(jpqlquery).anyTimes();
  EasyMock.expect(jpqlquery.setParameter(EasyMock.anyInt(), (Calendar) EasyMock.anyObject(), 
      EasyMock.anyObject(TemporalType.TIMESTAMP.getClass()))).andReturn(jpqlquery).anyTimes();
  EasyMock.expect(jpqlquery.setParameter(EasyMock.anyInt(), (Time) EasyMock.anyObject(), 
      EasyMock.anyObject(TemporalType.TIME.getClass()))).andReturn(jpqlquery).anyTimes();
  List<Object> result = new ArrayList<Object>();
  result.add(5);
  EasyMock.expect(jpqlquery.getResultList()).andReturn(result).anyTimes();
  EasyMock.replay(em, mm, jpqlquery);
  return em;

}
 
Example #6
Source File: AbstractModelService.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
private <T> String getTableName(EntityManager em, Class<T> entityClass){
	/*
	 * Check if the specified class is present in the metamodel.
	 * Throws IllegalArgumentException if not.
	 */
	Metamodel meta = em.getMetamodel();
	EntityType<T> entityType = meta.entity(entityClass);
	
	//Check whether @Table annotation is present on the class.
	Table t = entityClass.getAnnotation(Table.class);
	
	String tableName = (t == null) ? entityType.getName().toUpperCase() : t.name();
	return tableName;
}
 
Example #7
Source File: CustomRepositoryRestConfigurerAdapter.java    From spring-boot-jpa-data-rest-soft-delete with MIT License 5 votes vote down vote up
private List<Class<?>> getAllManagedEntityTypes(EntityManagerFactory entityManagerFactory) {
	List<Class<?>> entityClasses = new ArrayList<>();
	Metamodel metamodel = entityManagerFactory.getMetamodel();

	for (ManagedType<?> managedType : metamodel.getManagedTypes())
		if (managedType.getJavaType().isAnnotationPresent(Entity.class))
			entityClasses.add(managedType.getJavaType());

	return entityClasses;
}
 
Example #8
Source File: EntityModel.java    From webanno with Apache License 2.0 5 votes vote down vote up
private void analyze(T aObject)
{
    if (aObject != null) {
        entityClass = HibernateProxyHelper.getClassWithoutInitializingProxy(aObject);

        String idProperty = null;
        Metamodel metamodel = getEntityManager().getMetamodel();
        EntityType entity = metamodel.entity(entityClass);
        Set<SingularAttribute> singularAttributes = entity.getSingularAttributes();
        for (SingularAttribute singularAttribute : singularAttributes) {
            if (singularAttribute.isId()) {
                idProperty = singularAttribute.getName();
                break;
            }
        }
        if (idProperty == null) {
            throw new RuntimeException("id field not found");
        }

        DirectFieldAccessor accessor = new DirectFieldAccessor(aObject);
        id = (Number) accessor.getPropertyValue(idProperty);
    }
    else {
        entityClass = null;
        id = null;
    }
}
 
Example #9
Source File: JtaEntityManager.java    From tomee with Apache License 2.0 5 votes vote down vote up
public Metamodel getMetamodel() {
    final EntityManager entityManager = getEntityManager();
    try {
        final Timer timer = Op.getMetamodel.start(this.timer, this);
        try {
            return entityManager.getMetamodel();
        } finally {
            timer.stop();
        }
    } finally {
        closeIfNoTx(entityManager);
    }
}
 
Example #10
Source File: JPAEdmModel.java    From cloud-odata-java with Apache License 2.0 4 votes vote down vote up
public JPAEdmModel(final Metamodel metaModel, final String pUnitName) {
  super(metaModel, pUnitName);
}
 
Example #11
Source File: MetamodelQueryFinder.java    From jdal with Apache License 2.0 4 votes vote down vote up
public MetamodelQueryFinder(Metamodel metamodel) {
	this.metamodel = metamodel;
}
 
Example #12
Source File: CarsODataJPAServiceFactory.java    From tutorials with MIT License 4 votes vote down vote up
public Metamodel getMetamodel() {
    return delegate.getMetamodel();
}
 
Example #13
Source File: JPAEdmBaseViewImpl.java    From cloud-odata-java with Apache License 2.0 4 votes vote down vote up
public JPAEdmBaseViewImpl(final Metamodel metaModel, final String pUnitName) {
  this.metaModel = metaModel;
  this.pUnitName = pUnitName;
}
 
Example #14
Source File: EntityManagerDelegateHandler.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Override
public Metamodel getMetamodel()
{
    return entityManager().getMetamodel();
}
 
Example #15
Source File: JPAEdmComplexTypeTest.java    From cloud-odata-java with Apache License 2.0 4 votes vote down vote up
@Override
public Metamodel getJPAMetaModel() {
  return new JPAEdmMetaModel();
}
 
Example #16
Source File: JPAEdmSchemaTest.java    From cloud-odata-java with Apache License 2.0 4 votes vote down vote up
@Override
public Metamodel getJPAMetaModel() {
  return new JPAMetaModelMock();
}
 
Example #17
Source File: JPAProcessorImplTest.java    From cloud-odata-java with Apache License 2.0 4 votes vote down vote up
private Metamodel mockMetaModel() {
  Metamodel metaModel = EasyMock.createMock(Metamodel.class);
  EasyMock.replay(metaModel);
  return metaModel;
}
 
Example #18
Source File: ODataJPAProcessorDefaultTest.java    From cloud-odata-java with Apache License 2.0 4 votes vote down vote up
private Metamodel mockMetaModel() {
  Metamodel metaModel = EasyMock.createMock(Metamodel.class);
  EasyMock.expect(metaModel.getEntities()).andStubReturn(getLocalEntities());
  EasyMock.replay(metaModel);
  return metaModel;
}
 
Example #19
Source File: TestEntityManager.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@Override
public Metamodel getMetamodel()
{
    throw new IllegalStateException("not implemented");
}
 
Example #20
Source File: JPQLBuilderFactoryTest.java    From cloud-odata-java with Apache License 2.0 4 votes vote down vote up
@Test
public void testOdataJpaAccessFactory() {

  ODataJPAFactoryImpl oDataJPAFactoryImpl = new ODataJPAFactoryImpl();
  ODataJPAAccessFactory jpaAccessFactory = oDataJPAFactoryImpl
      .getODataJPAAccessFactory();
  ODataJPAContextImpl oDataJPAContextImpl = new ODataJPAContextImpl();

  EntityManagerFactory emf = new EntityManagerFactory() {

    @Override
    public boolean isOpen() {
      // TODO Auto-generated method stub
      return false;
    }

    @Override
    public Map<String, Object> getProperties() {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public PersistenceUnitUtil getPersistenceUnitUtil() {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public Metamodel getMetamodel() {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public CriteriaBuilder getCriteriaBuilder() {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public Cache getCache() {
      // TODO Auto-generated method stub
      return null;
    }

    @SuppressWarnings("rawtypes")
    @Override
    public EntityManager createEntityManager(final Map arg0) {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public EntityManager createEntityManager() {
      // TODO Auto-generated method stub
      return null;
    }

    @Override
    public void close() {
      // TODO Auto-generated method stub

    }
  };
  oDataJPAContextImpl.setEntityManagerFactory(emf);
  oDataJPAContextImpl.setPersistenceUnitName("pUnit");

  assertNotNull(jpaAccessFactory.getODataJPAMessageService(new Locale(
      "en")));
  assertNotNull(jpaAccessFactory.createODataJPAContext());
  assertNotNull(jpaAccessFactory
      .createJPAEdmProvider(oDataJPAContextImpl));
  assertNotNull(jpaAccessFactory
      .createODataProcessor(oDataJPAContextImpl));

}
 
Example #21
Source File: ReloadableEntityManagerFactory.java    From tomee with Apache License 2.0 4 votes vote down vote up
@Override
public Metamodel getMetamodel() {
    return delegate().getMetamodel();
}
 
Example #22
Source File: QueryLogEntityManager.java    From tomee with Apache License 2.0 4 votes vote down vote up
@Override
public Metamodel getMetamodel() {
    return delegate.getMetamodel();
}
 
Example #23
Source File: SharedEntityManagerFactoryProxy.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public Metamodel getMetamodel() {
	return emf().getMetamodel();
}
 
Example #24
Source File: EntityManagerWrapper.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public Metamodel getMetamodel() {
	return em.getMetamodel();
}
 
Example #25
Source File: SharedContextAwareEntityManagerProxy.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public Metamodel getMetamodel() {
	return em().getMetamodel();
}
 
Example #26
Source File: TestEntityManagerFactory.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Metamodel getMetamodel( )
{
    // TODO Auto-generated method stub
    return null;
}
 
Example #27
Source File: TestEntityManager.java    From lutece-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public Metamodel getMetamodel( )
{
    // TODO Auto-generated method stub
    return null;
}
 
Example #28
Source File: AbstractPlainJpa.java    From vaadinator with Apache License 2.0 4 votes vote down vote up
@Override
public Metamodel getMetamodel() {
	return entityManagers.get().getMetamodel();
}
 
Example #29
Source File: JPAProcessorImplTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
private Metamodel mockMetaModel() {
  Metamodel metaModel = EasyMock.createMock(Metamodel.class);
  EasyMock.replay(metaModel);
  return metaModel;
}
 
Example #30
Source File: JPAEdmSchemaTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Override
public Metamodel getJPAMetaModel() {
  return new JPAMetaModelMock();
}