Java Code Examples for javax.persistence.metamodel.EntityType#getName()

The following examples show how to use javax.persistence.metamodel.EntityType#getName() . 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: JPAPersistenceManager.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
private synchronized Integer getPKValue(EntityType targetEntity, String keyColumn, EntityManager entityManager) {
	logger.debug("IN");
	Integer toReturn = 0;
	String name = targetEntity.getName();

	logger.debug("SELECT max(p." + keyColumn + ") as c FROM " + targetEntity.getName() + " p");
	// logger.debug("SELECT max(p."+keyColumn+") as c FROM "+targetEntity.getName()+" p");
	Query maxQuery = entityManager.createQuery("SELECT max(p." + keyColumn + ") as c FROM " + targetEntity.getName() + " p");

	Object result = maxQuery.getSingleResult();

	if (result != null) {
		toReturn = Integer.valueOf(result.toString());
		toReturn++;
	}

	logger.debug("New PK is " + toReturn);
	logger.debug("OUT");
	return toReturn;
}
 
Example 2
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 3
Source File: CacheConfiguration.java    From angularjs-springboot-bookstore with MIT License 6 votes vote down vote up
@Bean
public CacheManager cacheManager() {
    log.debug("Starting Ehcache");
    cacheManager = net.sf.ehcache.CacheManager.create();
    cacheManager.getConfiguration().setMaxBytesLocalHeap(env.getProperty("cache.ehcache.maxBytesLocalHeap", String.class, "16M"));
    log.debug("Registering Ehcache Metrics gauges");
    Set<EntityType<?>> entities = entityManager.getMetamodel().getEntities();
    for (EntityType<?> entity : entities) {

        String name = entity.getName();
        if (name == null || entity.getJavaType() != null) {
            name = entity.getJavaType().getName();
        }
        Assert.notNull(name, "entity cannot exist without a identifier");

        net.sf.ehcache.Cache cache = cacheManager.getCache(name);
        if (cache != null) {
            cache.getCacheConfiguration().setTimeToLiveSeconds(env.getProperty("cache.timeToLiveSeconds", Long.class, 3600L));
            net.sf.ehcache.Ehcache decoratedCache = InstrumentedEhcache.instrument(metricRegistry, cache);
            cacheManager.replaceCacheWithDecoratedCache(cache, decoratedCache);
        }
    }
    EhCacheCacheManager ehCacheManager = new EhCacheCacheManager();
    ehCacheManager.setCacheManager(cacheManager);
    return ehCacheManager;
}
 
Example 4
Source File: CacheConfiguration.java    From ServiceCutter with Apache License 2.0 6 votes vote down vote up
@Bean
public CacheManager cacheManager() {
    log.debug("Starting Ehcache");
    cacheManager = net.sf.ehcache.CacheManager.create();
    cacheManager.getConfiguration().setMaxBytesLocalHeap(env.getProperty("cache.ehcache.maxBytesLocalHeap", String.class, "16M"));
    log.debug("Registering Ehcache Metrics gauges");
    Set<EntityType<?>> entities = entityManager.getMetamodel().getEntities();
    for (EntityType<?> entity : entities) {

        String name = entity.getName();
        if (name == null || entity.getJavaType() != null) {
            name = entity.getJavaType().getName();
        }
        Assert.notNull(name, "entity cannot exist without a identifier");

        net.sf.ehcache.Cache cache = cacheManager.getCache(name);
        if (cache != null) {
            cache.getCacheConfiguration().setTimeToLiveSeconds(env.getProperty("cache.timeToLiveSeconds", Long.class, 3600L));
            net.sf.ehcache.Ehcache decoratedCache = InstrumentedEhcache.instrument(metricRegistry, cache);
            cacheManager.replaceCacheWithDecoratedCache(cache, decoratedCache);
        }
    }
    EhCacheCacheManager ehCacheManager = new EhCacheCacheManager();
    ehCacheManager.setCacheManager(cacheManager);
    return ehCacheManager;
}
 
Example 5
Source File: JPAPersistenceManager.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void deleteRecord(JSONObject aRecord, RegistryConfiguration registryConf) {

	EntityTransaction entityTransaction = null;

	logger.debug("IN");
	EntityManager entityManager = null;
	try {
		Assert.assertNotNull(aRecord, "Input parameter [record] cannot be null");
		Assert.assertNotNull(aRecord, "Input parameter [registryConf] cannot be null");

		logger.debug("Record: " + aRecord.toString(3));
		logger.debug("Target entity: " + registryConf.getEntity());

		entityManager = dataSource.getEntityManager();
		Assert.assertNotNull(entityManager, "entityManager cannot be null");

		entityTransaction = entityManager.getTransaction();

		EntityType targetEntity = getTargetEntity(registryConf, entityManager);
		String keyAttributeName = getKeyAttributeName(targetEntity);
		logger.debug("Key attribute name is equal to " + keyAttributeName);

		Iterator it = aRecord.keys();

		Object keyColumnValue = aRecord.get(keyAttributeName);
		logger.debug("Key of record is equal to " + keyColumnValue);
		logger.debug("Key column java type equal to [" + targetEntity.getJavaType() + "]");
		Attribute a = targetEntity.getAttribute(keyAttributeName);
		Object obj = entityManager.find(targetEntity.getJavaType(), this.convertValue(keyColumnValue, a));
		logger.debug("Key column class is equal to [" + obj.getClass().getName() + "]");

		if (!entityTransaction.isActive()) {
			entityTransaction.begin();
		}

		// String q =
		// "DELETE from "+targetEntity.getName()+" o WHERE o."+keyAttributeName+"="+keyColumnValue.toString();
		String q = "DELETE from " + targetEntity.getName() + " WHERE " + keyAttributeName + "=" + keyColumnValue.toString();
		logger.debug("create Query " + q);
		Query deleteQuery = entityManager.createQuery(q);

		int deleted = deleteQuery.executeUpdate();

		// entityManager.remove(obj);
		// entityManager.flush();
		entityTransaction.commit();

	} catch (Throwable t) {
		if (entityTransaction != null && entityTransaction.isActive()) {
			entityTransaction.rollback();
		}
		logger.error(t);
		throw new SpagoBIRuntimeException("Error deleting entity", t);
	} finally {
		if (entityManager != null) {
			if (entityManager.isOpen()) {
				entityManager.close();
			}
		}
		logger.debug("OUT");
	}

}