List of usage examples for javax.persistence.metamodel Attribute getJavaMember
java.lang.reflect.Member getJavaMember();
java.lang.reflect.Member
for the represented attribute. From source file:com.impetus.client.cassandra.query.CassQuery.java
/** * Gets the compound key column.//from ww w .ja va 2 s .com * * @param metamodel * the metamodel * @param keyObj * the key obj * @param builder * the builder * @param isPresent * the is present * @param translator * the translator * @param fieldName * the field name * @param condition * the condition * @param value * the value * @param useInClause * the use in clause * @return the compound key column */ private boolean getCompoundKeyColumn(MetamodelImpl metamodel, EmbeddableType keyObj, StringBuilder builder, boolean isPresent, CQLTranslator translator, String fieldName, String condition, List<Object> value, boolean useInClause) { fieldName = fieldName.substring(fieldName.indexOf(".") + 1); // If partition key part age given in query, i.e. restriction on // id.compositekey.compositePartitionkey.partitionkeyColumn. if (fieldName.indexOf(".") > 0) { String compositePartitionkeyName = fieldName.substring(0, fieldName.indexOf(".")); AbstractAttribute attribute = (AbstractAttribute) keyObj.getAttribute(compositePartitionkeyName); fieldName = fieldName.substring(fieldName.indexOf(".") + 1); EmbeddableType compositePartitionkey = metamodel.embeddable(attribute.getBindableJavaType()); attribute = (AbstractAttribute) compositePartitionkey.getAttribute(fieldName); String columnName = attribute.getJPAColumnName(); isPresent = buildWhereClause(builder, isPresent, translator, condition, value, useInClause, attribute, columnName, false); } // if composite partition key object is given in query, i.e. restriction // on id.compositekey.compositePartitionkey else if (metamodel .isEmbeddable(((AbstractAttribute) keyObj.getAttribute(fieldName)).getBindableJavaType())) { AbstractAttribute attribute = (AbstractAttribute) keyObj.getAttribute(fieldName); Set<Attribute> attributes = metamodel.embeddable(attribute.getBindableJavaType()).getAttributes(); if (!useInClause) { // Iterating and appending each column of composite partition // key in query builder. for (Attribute nestedAttribute : attributes) { String columnName = ((AbstractAttribute) nestedAttribute).getJPAColumnName(); Object valueObject = PropertyAccessorHelper.getObject(value.isEmpty() ? null : value.get(0), (Field) nestedAttribute.getJavaMember()); translator.buildWhereClause(builder, nestedAttribute.getJavaType(), columnName, valueObject, condition, false); } } else { throw new IllegalArgumentException("In clause is not supported on first part of partition key."); } isPresent = true; } // if Not a composite partition key, // id.compositekey.partitionkey/clusterKey. else { AbstractAttribute attribute = (AbstractAttribute) keyObj.getAttribute(fieldName); String columnName = attribute.getJPAColumnName(); isPresent = buildWhereClause(builder, isPresent, translator, condition, value, useInClause, attribute, columnName, false); } return isPresent; }
From source file:com.impetus.client.oraclenosql.OracleNoSQLClient.java
/** * Delete by primary key.//w w w . j a v a 2 s. c om * * @param entity * the entity * @param pKey * the key */ @Override public void delete(Object entity, Object pKey) { EntityMetadata entityMetadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata, entity.getClass()); MetamodelImpl metamodel = (MetamodelImpl) KunderaMetadataManager.getMetamodel(kunderaMetadata, entityMetadata.getPersistenceUnit()); String idColumnName = ((AbstractAttribute) entityMetadata.getIdAttribute()).getJPAColumnName(); Table schemaTable = tableAPI.getTable(entityMetadata.getTableName()); PrimaryKey key = schemaTable.createPrimaryKey(); if (metamodel.isEmbeddable(entityMetadata.getIdAttribute().getBindableJavaType())) { EmbeddableType embeddableId = metamodel .embeddable(entityMetadata.getIdAttribute().getBindableJavaType()); Set<Attribute> embeddedAttributes = embeddableId.getAttributes(); for (Attribute embeddedAttrib : embeddedAttributes) { String columnName = ((AbstractAttribute) embeddedAttrib).getJPAColumnName(); Object embeddedColumn = PropertyAccessorHelper.getObject(pKey, (Field) embeddedAttrib.getJavaMember()); NoSqlDBUtils.add(schemaTable.getField(columnName), key, embeddedColumn, columnName); } } else { NoSqlDBUtils.add(schemaTable.getField(idColumnName), key, pKey, idColumnName); } tableAPI.delete(key, null, null); KunderaCoreUtils.printQuery("Delete data from " + entityMetadata.getTableName() + " for PK " + pKey, showQuery); }
From source file:com.impetus.client.oraclenosql.OracleNoSQLClient.java
/** * On embeddable id.//from ww w . j a va 2s .c o m * * @param entityMetadata * the entity metadata * @param metaModel * the meta model * @param schemaTable * the schema table * @param entity * the entity * @param row * the row * @throws InstantiationException * the instantiation exception * @throws IllegalAccessException * the illegal access exception */ private void onEmbeddableId(EntityMetadata entityMetadata, MetamodelImpl metaModel, Table schemaTable, Object entity, Row row) throws InstantiationException, IllegalAccessException { FieldDef fieldMetadata; FieldValue value; EmbeddableType embeddableType = metaModel.embeddable(entityMetadata.getIdAttribute().getBindableJavaType()); Set<Attribute> embeddedAttributes = embeddableType.getAttributes(); Object embeddedObject = entityMetadata.getIdAttribute().getBindableJavaType().newInstance(); for (Attribute attrib : embeddedAttributes) { String columnName = ((AbstractAttribute) attrib).getJPAColumnName(); fieldMetadata = schemaTable.getField(columnName); value = row.get(columnName); NoSqlDBUtils.get(fieldMetadata, value, embeddedObject, (Field) attrib.getJavaMember()); } PropertyAccessorHelper.set(entity, (Field) entityMetadata.getIdAttribute().getJavaMember(), embeddedObject); }
From source file:com.impetus.client.oraclenosql.OracleNoSQLClient.java
/** * Process embeddable attribute.//from www . j a v a2 s .c o m * * @param entity * the entity * @param metamodel * the metamodel * @param row * the row * @param schemaTable * the schema table * @param metadata * the metadata * @param attribute * the attribute */ private void processEmbeddableAttribute(Object entity, MetamodelImpl metamodel, Row row, Table schemaTable, EntityMetadata metadata, Attribute attribute) { // process on embeddables. EmbeddableType embeddable = metamodel.embeddable(((AbstractAttribute) attribute).getBindableJavaType()); Set<Attribute> embeddedAttributes = embeddable.getAttributes(); Object embeddedObject = PropertyAccessorHelper.getObject(entity, (Field) attribute.getJavaMember()); for (Attribute embeddedAttrib : embeddedAttributes) { setField(row, schemaTable, embeddedObject, embeddedAttrib); } }
From source file:com.impetus.client.cassandra.query.CassQuery.java
/** * Extract composite key./*from ww w . j a v a 2 s.co m*/ * * @param metaModel * the meta model * @param keyObj * the key obj * @param builder * the builder * @param translator * the translator * @param value * the value * @param useInClause * the use in clause * @param columnValues * the column values * @param field * the field * @return true, if successful */ private boolean extractCompositeKey(MetamodelImpl metaModel, EmbeddableType keyObj, StringBuilder builder, CQLTranslator translator, List<Object> value, boolean useInClause, Map<Attribute, List<Object>> columnValues, Field field) { Attribute compositeColumn = keyObj.getAttribute(field.getName()); String jpaColumnName = ((AbstractAttribute) compositeColumn).getJPAColumnName(); if (useInClause) { for (Object embeddedObject : value) { Object valueObject = PropertyAccessorHelper.getObject(embeddedObject, field); // Checking for composite partition key. if (metaModel.isEmbeddable(((AbstractAttribute) compositeColumn).getBindableJavaType())) { Set<Attribute> attributes = metaModel .embeddable(((AbstractAttribute) compositeColumn).getBindableJavaType()) .getAttributes(); // Iterating over composite partition key columns. for (Attribute nestedAttribute : attributes) { List<Object> valueList = columnValues.get(compositeColumn); if (valueList == null) { valueList = new ArrayList<Object>(); } Object obj = PropertyAccessorHelper.getObject(valueObject, (Field) nestedAttribute.getJavaMember()); valueList.add(obj); columnValues.put(nestedAttribute, valueList); } } else { List<Object> valueList = columnValues.get(compositeColumn); if (valueList == null) { valueList = new ArrayList<Object>(); } valueList.add(valueObject); columnValues.put(compositeColumn, valueList); } } } else { Object valueObject = PropertyAccessorHelper.getObject(value.isEmpty() ? null : value.get(0), field); // Checking for composite partition key. if (metaModel.isEmbeddable(((AbstractAttribute) compositeColumn).getBindableJavaType())) { Set<Attribute> attributes = metaModel .embeddable(((AbstractAttribute) compositeColumn).getBindableJavaType()).getAttributes(); // Iterating over composite partition key columns. for (Attribute nestedAttribute : attributes) { String columnName = ((AbstractAttribute) nestedAttribute).getJPAColumnName(); Object obj = PropertyAccessorHelper.getObject(valueObject, (Field) nestedAttribute.getJavaMember()); translator.buildWhereClause(builder, nestedAttribute.getJavaType(), columnName, obj, CQLTranslator.EQ_CLAUSE, false); } // returning true because builder has AND clause at end. return true; } else { translator.buildWhereClause(builder, field.getType(), jpaColumnName, valueObject, CQLTranslator.EQ_CLAUSE, false); // returning true because builder has AND clause at end. return true; } } // returning false because builder does not have AND clause at end. return false; }
From source file:com.impetus.client.cassandra.query.CassQuery.java
/** * Returns bytes value for given value./*from w w w . ja v a 2s . co m*/ * * @param jpaFieldName * field name. * @param m * entity metadata * @param value * value. * @return bytes value. */ ByteBuffer getBytesValue(String jpaFieldName, EntityMetadata m, Object value) { Attribute idCol = m.getIdAttribute(); MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata() .getMetamodel(m.getPersistenceUnit()); EntityType entity = metaModel.entity(m.getEntityClazz()); Field f = null; boolean isId = false; if (((AbstractAttribute) idCol).getJPAColumnName().equals(jpaFieldName)) { f = (Field) idCol.getJavaMember(); isId = true; } else { if (jpaFieldName != null && jpaFieldName.indexOf(Constants.INDEX_TABLE_ROW_KEY_DELIMITER) > 0) { String embeddedFieldName = jpaFieldName.substring(0, jpaFieldName.indexOf(Constants.INDEX_TABLE_ROW_KEY_DELIMITER)); String columnFieldName = jpaFieldName.substring( jpaFieldName.indexOf(Constants.INDEX_TABLE_ROW_KEY_DELIMITER) + 1, jpaFieldName.length()); Attribute embeddedAttr = entity.getAttribute(embeddedFieldName); try { Class<?> embeddedClass = embeddedAttr.getJavaType(); if (Collection.class.isAssignableFrom(embeddedClass)) { Class<?> genericClass = PropertyAccessorHelper .getGenericClass((Field) embeddedAttr.getJavaMember()); f = genericClass.getDeclaredField(columnFieldName); } else { f = embeddedClass.getDeclaredField(columnFieldName); } } catch (SecurityException e) { log.error("Error while extrating " + jpaFieldName + ", Caused by: ", e); throw new QueryHandlerException("Error while extrating " + jpaFieldName + "."); } catch (NoSuchFieldException e) { log.error("Error while extrating " + jpaFieldName + ", Caused by: ", e); throw new QueryHandlerException("Error while extrating " + jpaFieldName + "."); } } else { String discriminatorColumn = ((AbstractManagedType) entity).getDiscriminatorColumn(); if (!jpaFieldName.equals(discriminatorColumn)) { String fieldName = m.getFieldName(jpaFieldName); Attribute col = entity.getAttribute(fieldName); if (col == null) { throw new QueryHandlerException("column type is null for: " + jpaFieldName); } f = (Field) col.getJavaMember(); } } } // need to do integer.parseInt..as value will be string in case of // create query. if (f != null && f.getType() != null) { return CassandraUtilities.toBytes(value, f); } else { // default is String type return CassandraUtilities.toBytes(value, String.class); } }
From source file:com.impetus.client.oraclenosql.OracleNoSQLClient.java
/** * Scroll and populate./*from w w w . j a v a 2 s . com*/ * * @param key * the key * @param entityMetadata * the entity metadata * @param metaModel * the meta model * @param schemaTable * the schema table * @param rowsIter * the rows iter * @param relationMap * the relation map * @param columnsToSelect * the columns to select * @return the list * @throws InstantiationException * the instantiation exception * @throws IllegalAccessException * the illegal access exception */ private List scrollAndPopulate(Object key, EntityMetadata entityMetadata, MetamodelImpl metaModel, Table schemaTable, Iterator<Row> rowsIter, Map<String, Object> relationMap, List<String> columnsToSelect) throws InstantiationException, IllegalAccessException { List results = new ArrayList(); Object entity = null; EntityType entityType = metaModel.entity(entityMetadata.getEntityClazz()); // here while (rowsIter.hasNext()) { relationMap = new HashMap<String, Object>(); entity = initializeEntity(key, entityMetadata); Row row = rowsIter.next(); List<String> fields = row.getTable().getFields(); FieldDef fieldMetadata = null; FieldValue value = null; String idColumnName = ((AbstractAttribute) entityMetadata.getIdAttribute()).getJPAColumnName(); if (/* eligibleToFetch(columnsToSelect, idColumnName) && */!metaModel .isEmbeddable(entityMetadata.getIdAttribute().getBindableJavaType())) { populateId(entityMetadata, schemaTable, entity, row); } else { onEmbeddableId(entityMetadata, metaModel, schemaTable, entity, row); } Iterator<String> fieldIter = fields.iterator(); Set<Attribute> attributes = entityType.getAttributes(); for (Attribute attribute : attributes) { String jpaColumnName = ((AbstractAttribute) attribute).getJPAColumnName(); if (eligibleToFetch(columnsToSelect, jpaColumnName) && !attribute.getName().equals(entityMetadata.getIdAttribute().getName())) { if (metaModel.isEmbeddable(((AbstractAttribute) attribute).getBindableJavaType())) { // readEmbeddable(value, columnsToSelect, // entityMetadata, metaModel, schemaTable, value, // attribute); EmbeddableType embeddableId = metaModel .embeddable(((AbstractAttribute) attribute).getBindableJavaType()); Set<Attribute> embeddedAttributes = embeddableId.getAttributes(); Object embeddedObject = ((AbstractAttribute) attribute).getBindableJavaType().newInstance(); for (Attribute embeddedAttrib : embeddedAttributes) { String embeddedColumnName = ((AbstractAttribute) embeddedAttrib).getJPAColumnName(); fieldMetadata = schemaTable.getField(embeddedColumnName); value = row.get(embeddedColumnName); NoSqlDBUtils.get(fieldMetadata, value, embeddedObject, (Field) embeddedAttrib.getJavaMember()); } PropertyAccessorHelper.set(entity, (Field) attribute.getJavaMember(), embeddedObject); } else { fieldMetadata = schemaTable.getField(jpaColumnName); value = row.get(jpaColumnName); if (!attribute.isAssociation() && value != null) { NoSqlDBUtils.get(fieldMetadata, value, entity, (Field) attribute.getJavaMember()); } else if (attribute.isAssociation() && value != null) { Relation relation = entityMetadata.getRelation(attribute.getName()); if (relation != null) { EntityMetadata associationMetadata = KunderaMetadataManager .getEntityMetadata(kunderaMetadata, relation.getTargetEntity()); if (!relation.getType().equals(ForeignKey.MANY_TO_MANY)) { relationMap.put(jpaColumnName, NoSqlDBUtils.get(fieldMetadata, value, (Field) associationMetadata.getIdAttribute().getJavaMember())); } } } } } } if (entity != null) { results.add(relationMap.isEmpty() ? entity : new EnhanceEntity(entity, key != null ? key : PropertyAccessorHelper.getId(entity, entityMetadata), relationMap)); } } return results; }
From source file:com.impetus.kundera.client.cassandra.dsdriver.DSClient.java
/** * Populate composite id./*from w w w .j ava 2 s. c o m*/ * * @param metadata * the metadata * @param entity * the entity * @param columnName * the column name * @param row * the row * @param metaModel * the meta model * @param attribute * the attribute * @param entityClazz * the entity clazz * @param dataType * the data type * @return the object */ private Object populateCompositeId(EntityMetadata metadata, Object entity, String columnName, Row row, MetamodelImpl metaModel, Attribute attribute, Class<?> entityClazz, DataType dataType) { Class javaType = ((AbstractAttribute) attribute).getBindableJavaType(); if (metaModel.isEmbeddable(javaType)) { EmbeddableType compoundKey = metaModel.embeddable(javaType); Object compoundKeyObject = null; try { Set<Attribute> attributes = compoundKey.getAttributes(); entity = KunderaCoreUtils.initialize(entityClazz, entity); for (Attribute compoundAttribute : attributes) { compoundKeyObject = compoundKeyObject == null ? getCompoundKey(attribute, entity) : compoundKeyObject; if (metaModel.isEmbeddable(((AbstractAttribute) compoundAttribute).getBindableJavaType())) { Object compoundObject = populateCompositeId(metadata, compoundKeyObject, columnName, row, metaModel, compoundAttribute, javaType, dataType); PropertyAccessorHelper.set(entity, (Field) attribute.getJavaMember(), compoundObject); } else if (((AbstractAttribute) compoundAttribute).getJPAColumnName().equals(columnName)) { DSClientUtilities.assign(row, compoundKeyObject, null, dataType.getName(), null, columnName, (Field) compoundAttribute.getJavaMember(), metaModel); PropertyAccessorHelper.set(entity, (Field) attribute.getJavaMember(), compoundKeyObject); break; } } } catch (IllegalArgumentException iaex) { // ignore as it might not represented within entity. // No need for any logger message } catch (Exception e) { log.error("Error while retrieving data, Caused by: .", e); throw new PersistenceException(e); } } return entity; }
From source file:com.impetus.client.redis.RedisClient.java
/** * Wraps entity attributes into redis format byte[]. * /*from w w w.ja v a 2 s. co m*/ * @param entityMetadata * the entity metadata * @param wrapper * the wrapper * @param embeddedObject * the embedded object * @param attrib * the attrib * @param embeddedAttrib * the embedded attrib */ private void addToWrapper(EntityMetadata entityMetadata, AttributeWrapper wrapper, Object embeddedObject, Attribute attrib, Attribute embeddedAttrib) { byte[] value = PropertyAccessorHelper.get(embeddedObject, (Field) attrib.getJavaMember()); byte[] name; if (value != null) { if (embeddedAttrib == null) { name = getEncodedBytes(((AbstractAttribute) attrib).getJPAColumnName()); } else { name = getEncodedBytes( getHashKey(embeddedAttrib.getName(), ((AbstractAttribute) attrib).getJPAColumnName())); } // add column name as key and value as value wrapper.addColumn(name, value); // // {tablename:columnname,hashcode} for value // selective indexing. if (entityMetadata.getIndexProperties().containsKey(((AbstractAttribute) attrib).getJPAColumnName())) { String valueAsStr = PropertyAccessorHelper.getString(embeddedObject, (Field) attrib.getJavaMember()); wrapper.addIndex( getHashKey(entityMetadata.getTableName(), ((AbstractAttribute) attrib).getJPAColumnName()), getDouble(valueAsStr)); wrapper.addIndex( getHashKey(entityMetadata.getTableName(), getHashKey(((AbstractAttribute) attrib).getJPAColumnName(), valueAsStr)), getDouble(valueAsStr)); } } }
From source file:com.impetus.client.redis.RedisClient.java
/** * Unwraps redis results into entity./* ww w . j a v a 2 s. com*/ * * @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; }