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

The following examples show how to use javax.persistence.metamodel.EntityType#getAttribute() . 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 void manageProperty(EntityType targetEntity, Object obj, String aKey, JSONObject aRecord) {

		logger.debug("IN");

		try {
			Attribute a = targetEntity.getAttribute(aKey);
			Class clas = targetEntity.getJavaType();
			Field f = clas.getDeclaredField(aKey);
			f.setAccessible(true);
			Object valueConverted = this.convertValue(aRecord.get(aKey), a);
			f.set(obj, valueConverted);
		} catch (Exception e) {
			throw new SpagoBIRuntimeException("Error setting Field " + aKey + "", e);
		} finally {
			logger.debug("OUT");
		}
	}
 
Example 2
Source File: JPAPersistenceManager.java    From Knowage-Server with GNU Affero General Public License v3.0 6 votes vote down vote up
private void setKeyProperty(EntityType targetEntity, Object obj, String aKey, Integer value) {

		logger.debug("IN");

		try {
			Attribute a = targetEntity.getAttribute(aKey);
			Class clas = targetEntity.getJavaType();
			Field f = clas.getDeclaredField(aKey);
			f.setAccessible(true);
			Object valueConverted = this.convertValue(value, a);
			f.set(obj, valueConverted);
		} catch (Exception e) {
			throw new SpagoBIRuntimeException("Error setting Field " + aKey + "", e);
		} finally {
			logger.debug("OUT");
		}
	}
 
Example 3
Source File: JPAPersistenceManager.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void updateRecord(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("New 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 new 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() + "]");

		while (it.hasNext()) {
			String attributeName = (String) it.next();
			logger.debug("Processing column [" + attributeName + "] ...");

			if (keyAttributeName.equals(attributeName)) {
				logger.debug("Skip column [" + attributeName + "] because it is the key of the table");
				continue;
			}
			Column column = registryConf.getColumnConfiguration(attributeName);
			if (!column.isEditable()) {
				logger.debug("Skip column [" + attributeName + "] because it is not editable");
				continue;
			}
			List columnDepends = new ArrayList();
			if (column.getDependences() != null && !"".equals(column.getDependences())) {
				String[] dependences = column.getDependences().split(",");
				for (int i = 0; i < dependences.length; i++) {
					// get dependences informations
					Column dependenceColumns = getDependenceColumns(registryConf.getColumns(), dependences[i]);
					if (dependenceColumns != null)
						columnDepends.add(dependenceColumns);
				}
			}

			// if column is info column do not update
			if (!column.isInfoColumn()) {
				if (column.getSubEntity() != null) {
					logger.debug("Column [" + attributeName + "] is a foreign key");
					manageForeignKey(targetEntity, column, obj, attributeName, aRecord, columnDepends, entityManager, registryConf.getColumns());
				} else {
					logger.debug("Column [" + attributeName + "] is a normal column");
					manageProperty(targetEntity, obj, attributeName, aRecord);
				}
			}
		}

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

		entityManager.persist(obj);
		entityManager.flush();
		entityTransaction.commit();

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

}
 
Example 4
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");
	}

}
 
Example 5
Source File: JpaDataFetcher.java    From graphql-jpa with MIT License 4 votes vote down vote up
private Attribute getAttribute(DataFetchingEnvironment environment, Argument argument) {
    GraphQLObjectType objectType = getObjectType(environment, argument);
    EntityType entityType = getEntityType(objectType);

    return entityType.getAttribute(argument.getName());
}
 
Example 6
Source File: JpaDao.java    From jdal with Apache License 2.0 4 votes vote down vote up
/**
 * Null References on one to many and one to one associations.
 * Will only work if association has annotated with a mappedBy attribute.
 * 
 * @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);
					}
				}
			}
		}
	}
}