List of usage examples for javax.persistence.metamodel EntityType getAttribute
Attribute<? super X, ?> getAttribute(String name);
From source file:com.impetus.client.redis.RedisClient.java
/** * Unwraps redis results into entity.//from w w w . j a v a 2 s . c o m * * @param entityMetadata * the entity metadata * @param results * the results * @param key * the key * @return the object * @throws InstantiationException * the instantiation exception * @throws IllegalAccessException * the illegal access exception */ private Object unwrap(EntityMetadata entityMetadata, Map<byte[], byte[]> results, Object key) throws InstantiationException, IllegalAccessException { MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata() .getMetamodel(entityMetadata.getPersistenceUnit()); List<String> relationNames = entityMetadata.getRelationNames(); EntityType entityType = metaModel.entity(entityMetadata.getEntityClazz()); Map<String, Object> relations = new HashMap<String, Object>(); Object entity = null; // Set<Attribute> attributes = entityType.getAttributes(); Set<byte[]> columnNames = results.keySet(); for (byte[] nameInByte : columnNames) { if (entity == null) { entity = KunderaCoreUtils.createNewInstance(entityMetadata.getEntityClazz()); } String columnName = PropertyAccessorFactory.STRING.fromBytes(String.class, nameInByte); byte[] value = results.get(nameInByte); String discriminatorColumn = ((AbstractManagedType) entityType).getDiscriminatorColumn(); if (columnName != null && !columnName.equals(discriminatorColumn)) { String fieldName = entityMetadata.getFieldName(columnName); if (fieldName != null) { Attribute attribute = entityType.getAttribute(fieldName); if (relationNames != null && relationNames.contains(columnName)) { Field field = (Field) attribute.getJavaMember(); EntityMetadata associationMetadata = KunderaMetadataManager.getEntityMetadata( kunderaMetadata, ((AbstractAttribute) attribute).getBindableJavaType()); relations.put(columnName, PropertyAccessorHelper .getObject(associationMetadata.getIdAttribute().getBindableJavaType(), value)); } else { PropertyAccessorHelper.set(entity, (Field) attribute.getJavaMember(), value); } } else { // means it might be an embeddable field, if not simply omit // this field. if (StringUtils.contains(columnName, ":")) { StringTokenizer tokenizer = new StringTokenizer(columnName, ":"); while (tokenizer.hasMoreTokens()) { String embeddedFieldName = tokenizer.nextToken(); String embeddedColumnName = tokenizer.nextToken(); Map<String, EmbeddableType> embeddables = metaModel .getEmbeddables(entityMetadata.getEntityClazz()); EmbeddableType embeddableAttribute = embeddables.get(embeddedFieldName); Attribute attrib = embeddableAttribute.getAttribute(embeddedColumnName); Object embeddedObject = PropertyAccessorHelper.getObject(entity, (Field) entityType.getAttribute(embeddedFieldName).getJavaMember()); if (embeddedObject == null) { embeddedObject = KunderaCoreUtils.createNewInstance( ((AbstractAttribute) entityType.getAttribute(embeddedFieldName)) .getBindableJavaType()); PropertyAccessorHelper.set(entity, (Field) entityType.getAttribute(embeddedFieldName).getJavaMember(), embeddedObject); } PropertyAccessorHelper.set(embeddedObject, (Field) attrib.getJavaMember(), value); // PropertyAccessorHelper. } } // It might be a case of embeddable attribute. } } } if (entity != null) { Class javaType = entityMetadata.getIdAttribute().getBindableJavaType(); if (!metaModel.isEmbeddable(entityMetadata.getIdAttribute().getBindableJavaType()) && key.getClass().isAssignableFrom(String.class) && !key.getClass().equals(javaType)) { key = PropertyAccessorFactory.getPropertyAccessor(javaType).fromString(javaType, key.toString()); } // PropertyAccessorHelper.set(entity, (Field) entityMetadata.getIdAttribute().getJavaMember(), key); } if (!relations.isEmpty()) { return new EnhanceEntity(entity, key, relations); } return entity; }
From source file:ru.savvy.jpafilterbuilder.FilterCriteriaBuilder.java
/** * Returns Java type of the fieldName/*w w w . jav a 2s. c o m*/ * * @param fieldName * @return * @throws IllegalArgumentException if fieldName isn't valid for given * entity */ public Class<?> getJavaType(String fieldName) { String[] compoundField = fieldName.split("\\."); EntityType et = metamodel.entity(clazz); for (int i = 0; i < compoundField.length; i++) { if (i < (compoundField.length - 1)) { try { Attribute att = et.getAttribute(compoundField[i]); if (att.isCollection()) { et = metamodel.entity(getPluralJavaType(et, compoundField[i])); } else { et = metamodel.entity(et.getAttribute(compoundField[i]).getJavaType()); } } catch (IllegalArgumentException | IllegalStateException e) { throw new IllegalArgumentException("Illegal field name " + fieldName + " (" + compoundField[i] + ") for root type " + clazz); } } else { try { return et.getAttribute(compoundField[i]).getJavaType(); } catch (IllegalArgumentException | IllegalStateException e) { throw new IllegalArgumentException("Illegal field name " + fieldName + " (" + compoundField[i] + ") for root type " + clazz); } } } return null; // should never be reached }