List of usage examples for javax.persistence.metamodel Attribute getName
String getName();
From source file:com.impetus.client.cassandra.CassandraClientBase.java
/** * Return update query string for given entity intended for counter column * family./*from w ww . ja v a2 s. co m*/ * * @param entityMetadata * the entity metadata * @param entity * the entity * @param cassandra_client * the cassandra_client * @param rlHolders * the rl holders * @return the list */ protected List<String> createUpdateQueryForCounter(EntityMetadata entityMetadata, Object entity, Cassandra.Client cassandra_client, List<RelationHolder> rlHolders) { Map<String, String> builders = new HashMap<String, String>(); CQLTranslator translator = new CQLTranslator(); Object rowId = PropertyAccessorHelper.getId(entity, entityMetadata); MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata() .getMetamodel(entityMetadata.getPersistenceUnit()); EntityType entityType = metaModel.entity(entityMetadata.getEntityClazz()); Set<Attribute> attributes = entityType.getAttributes(); for (Attribute attrib : attributes) { if (!entityMetadata.getIdAttribute().getName().equals(attrib.getName()) && !metaModel.isEmbeddable(attrib.getJavaType()) && !attrib.isAssociation()) { String tableName = ((AbstractAttribute) attrib).getTableName() != null ? ((AbstractAttribute) attrib).getTableName() : entityMetadata.getTableName(); String queryString = builders.get(tableName); StringBuilder builder; if (queryString == null) { builder = new StringBuilder(); } else { builder = new StringBuilder(queryString); } translator.buildSetClauseForCounters(builder, ((AbstractAttribute) attrib).getJPAColumnName(), PropertyAccessorHelper.getObject(entity, attrib.getName())); builders.put(tableName, builder.toString()); } } for (RelationHolder rl : rlHolders) { translator.buildSetClauseForCounters(new StringBuilder(builders.get(entityMetadata.getTableName())), rl.getRelationName(), rl.getRelationValue()); } for (String tableName : builders.keySet()) { StringBuilder builder = new StringBuilder(builders.get(tableName)); String update_Query = translator.UPDATE_QUERY; update_Query = StringUtils.replace(update_Query, CQLTranslator.COLUMN_FAMILY, translator.ensureCase(new StringBuilder(), tableName, false).toString()); // strip last "," clause. builder.delete(builder.lastIndexOf(CQLTranslator.COMMA_STR), builder.length()); builder.append(CQLTranslator.ADD_WHERE_CLAUSE); onWhereClause(entityMetadata, rowId, translator, builder, metaModel, entityMetadata.getIdAttribute()); // strip last "AND" clause. builder.delete(builder.lastIndexOf(CQLTranslator.AND_CLAUSE), builder.length()); StringBuilder queryBuilder = new StringBuilder(update_Query); queryBuilder.append(CQLTranslator.ADD_SET_CLAUSE); queryBuilder.append(builder); if (log.isDebugEnabled()) { log.debug("Returning update query {}.", queryBuilder.toString()); } builders.put(tableName, queryBuilder.toString()); } return new ArrayList(builders.values()); }
From source file:com.impetus.client.cassandra.schemamanager.CassandraSchemaManager.java
/** * Creates the typefor embeddables./* w ww. j a va2s.c om*/ * */ private void createTypeforEmbeddables() { if (!createdPuEmbeddables.contains(puMetadata.getPersistenceUnitName())) { CQLTranslator translator = new CQLTranslator(); Map<String, String> embNametoUDTQuery = new HashMap<String, String>(); Map<String, List<String>> embNametoDependentList = new HashMap<String, List<String>>(); MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata() .getMetamodel(puMetadata.getPersistenceUnitName()); Iterator iter = metaModel.getEmbeddables().iterator(); while (iter.hasNext()) { List<String> childEmb = new ArrayList<String>(); String typeQuery = CQLTranslator.CREATE_TYPE; EmbeddableType embeddedColumn = (EmbeddableType) iter.next(); if (!embeddedColumn.getPersistenceType().equals(PersistenceType.EMBEDDABLE)) { continue; } typeQuery = StringUtils.replace(typeQuery, CQLTranslator.TYPE, translator .ensureCase(new StringBuilder(), embeddedColumn.getJavaType().getSimpleName(), false) .toString()); StringBuilder typeQueryBuilder = new StringBuilder(); for (Object column : embeddedColumn.getAttributes()) { Attribute columnAttribute = (Attribute) column; Field f = (Field) columnAttribute.getJavaMember(); if (columnAttribute.getJavaType().isAnnotationPresent(Embeddable.class)) { // handle embeddable String cqlType = CQLTranslator.FROZEN + Constants.STR_LT + Constants.ESCAPE_QUOTE + columnAttribute.getJavaType().getSimpleName() + Constants.ESCAPE_QUOTE + Constants.STR_GT; translator.appendColumnName(typeQueryBuilder, columnAttribute.getName(), cqlType); typeQueryBuilder.append(Constants.SPACE_COMMA); childEmb.add(columnAttribute.getJavaType().getSimpleName()); } else if (columnAttribute.isCollection()) { // handle element collection with embeddables handleElementCollectionAttribute(translator, columnAttribute, typeQueryBuilder); if (!MetadataUtils.isBasicElementCollectionField((Field) columnAttribute.getJavaMember())) { childEmb.add( ((AbstractAttribute) columnAttribute).getBindableJavaType().getSimpleName()); } } else { String cqlType = null; String dataType = CassandraValidationClassMapper.getValidationClass(f.getType(), true); cqlType = translator.getCQLType(dataType); // check for JPA names translator.appendColumnName(typeQueryBuilder, ((AbstractAttribute) columnAttribute).getJPAColumnName(), cqlType); typeQueryBuilder.append(Constants.SPACE_COMMA); } } typeQueryBuilder = replaceColumnsAndStripLastChar(typeQuery, typeQueryBuilder); typeQueryBuilder.append(CQLTranslator.CLOSE_BRACKET); embNametoUDTQuery.put(embeddedColumn.getJavaType().getSimpleName(), typeQueryBuilder.toString()); embNametoDependentList.put(embeddedColumn.getJavaType().getSimpleName(), childEmb); // run query final } postProcessEmbedded(embNametoUDTQuery, embNametoDependentList); createdPuEmbeddables.add(puMetadata.getPersistenceUnitName()); } }
From source file:nl.strohalm.cyclos.utils.database.DatabaseQueryHandler.java
@SuppressWarnings("unchecked") public void resolveReferences(final Entity entity) { final EntityType meta = getClassMetaData(entity); Set<Attribute> attrs = meta.getAttributes(); for (Attribute attr : attrs) { final Attribute type = attr; final String name = attr.getName(); if (type instanceof EntityType) { // Properties that are relationships to other entities Entity rel = PropertyHelper.get(entity, name); if (rel instanceof EntityReference) { rel = DatabaseUtil.getCurrentEntityManager().find(EntityHelper.getRealClass(rel), rel.getId()); PropertyHelper.set(entity, name, rel); }/*w ww . jav a 2 s .c o m*/ } else if (type.isCollection()) { // Properties that are collections of other entities final Collection<?> current = PropertyHelper.get(entity, name); if (current != null /*&& !(current instanceof PersistentCollection)*/) { // We must check that the collection is made of entities, since Hibernate supports collections os values boolean isEntityCollection = true; final Collection<Entity> resolved = ClassHelper.instantiate(current.getClass()); for (final Object object : current) { if (object != null && !(object instanceof Entity)) { isEntityCollection = false; break; } Entity e = (Entity) object; if (object instanceof EntityReference) { e = DatabaseUtil.getCurrentEntityManager().find(EntityHelper.getRealClass(e), e.getId()); } resolved.add(e); } if (isEntityCollection) { PropertyHelper.set(entity, name, resolved); } } } } }
From source file:org.hibernate.ejb.metamodel.EntityTypeImpl.java
public Set<Attribute<? super X, ?>> getSortedAttributes() { Set<Attribute<? super X, ?>> sortedAttributes = new TreeSet<Attribute<? super X, ?>>( new AttributeComparator()); for (Attribute<? super X, ?> attribute : this.getAttributes()) { ((AbstractAttribute) attribute).setWeight( DomainAttributeSequences.getWeight(this.getJavaType().getSimpleName(), attribute.getName())); sortedAttributes.add(attribute); }/*from w w w.j av a 2s.c o m*/ return sortedAttributes; }
From source file:org.makersoft.activerecord.jpa.JPQL.java
@SuppressWarnings({ "rawtypes", "unchecked" }) private Set<String> getColumns() { Metamodel metamodel = em().getMetamodel(); Set<String> columns = new HashSet<String>(); for (EntityType entityType : metamodel.getEntities()) { if (entityName.equals(entityType.getName())) { Set<Attribute> attributes = entityType.getAttributes(); for (Attribute attribute : attributes) { columns.add(attribute.getName()); }/*from w w w . java 2 s .co m*/ } } return columns; }