List of usage examples for javax.persistence.metamodel EntityType getAttributes
Set<Attribute<? super X, ?>> getAttributes();
From source file:org.jdal.dao.jpa.JpaUtils.java
/** * Get all attributes where type or element type is assignable from class and has persistent type * @param type entity type/* w ww . jav a2s . c o m*/ * @param persistentType persistentType * @param clazz class * @return Set with matching attributes */ public static Set<Attribute<?, ?>> getAttributes(EntityType<?> type, PersistentAttributeType persistentType, Class<?> clazz) { Set<Attribute<?, ?>> attributes = new HashSet<Attribute<?, ?>>(); for (Attribute<?, ?> a : type.getAttributes()) { if (a.getPersistentAttributeType() == persistentType && isTypeOrElementType(a, clazz)) { attributes.add(a); } } return attributes; }
From source file:com.impetus.kundera.metadata.MetadataUtils.java
private static void getEmbeddableType(EntityMetadata m, Map<String, Field> columnNameToFieldMap, Map<String, Field> superColumnNameToFieldMap, final KunderaMetadata kunderaMetadata) { Metamodel metaModel = kunderaMetadata.getApplicationMetadata().getMetamodel(m.getPersistenceUnit()); EntityType entityType = metaModel.entity(m.getEntityClazz()); Set attributes = entityType.getAttributes(); Iterator<Attribute> iter = attributes.iterator(); while (iter.hasNext()) { Attribute attribute = iter.next(); if (((MetamodelImpl) metaModel).isEmbeddable(((AbstractAttribute) attribute).getBindableJavaType())) { superColumnNameToFieldMap.put(((AbstractAttribute) attribute).getJPAColumnName(), (Field) attribute.getJavaMember()); if (columnNameToFieldMap != null) { getAttributeOfEmbedddable(columnNameToFieldMap, metaModel, attribute); }/*from ww w . j ava 2 s .c o m*/ } else { if (columnNameToFieldMap != null) { columnNameToFieldMap.put(((AbstractAttribute) attribute).getJPAColumnName(), (Field) attribute.getJavaMember()); } } } }
From source file:com.impetus.kundera.metadata.MetadataUtils.java
/** * Checks whether an entity with given metadata contains a collection field * //from ww w .ja va 2 s .c o m * @param m * @return */ public static boolean containsBasicElementCollectionField(final EntityMetadata m, final KunderaMetadata kunderaMetadata) { Metamodel metaModel = kunderaMetadata.getApplicationMetadata().getMetamodel(m.getPersistenceUnit()); EntityType entityType = metaModel.entity(m.getEntityClazz()); Iterator<Attribute> iter = entityType.getAttributes().iterator(); while (iter.hasNext()) { Attribute attr = iter.next(); if (attr.isCollection() && !attr.isAssociation() && isBasicElementCollectionField((Field) attr.getJavaMember())) { return true; } } return false; }
From source file:com.impetus.kundera.metadata.MetadataUtils.java
public static void onJPAColumnMapping(final EntityType entityType, EntityMetadata entityMetadata) { Set<Attribute> attributes = entityType.getAttributes(); Iterator<Attribute> iter = attributes.iterator(); while (iter.hasNext()) { Attribute attribute = iter.next(); // jpa column mapping is for non id columns only. if (!entityMetadata.getIdAttribute().equals(attribute)) { entityMetadata.addJPAColumnMapping(((AbstractAttribute) attribute).getJPAColumnName(), attribute.getName()); }/* w w w. j a va 2 s . co m*/ } entityMetadata.setEntityType(entityType); }
From source file:com.github.gekoh.yagen.util.MappingUtils.java
public static Map<Class, Set<Class>> getMasterToDetailClassesMap(EntityManagerFactory emf) throws Exception { Map<Class, TreeEntity> treeEntities = new HashMap<Class, TreeEntity>(); for (EntityType entityType : emf.getMetamodel().getEntities()) { Class entityClass = entityType.getJavaType(); if (!treeEntities.containsKey(entityClass)) { treeEntities.put(entityClass, new TreeEntity(entityClass)); }//from w w w. j a v a 2s . com fillTreeEntityMap(treeEntities, entityType.getAttributes(), entityClass); } Map<Class, Set<Class>> m2dMap = new HashMap<Class, Set<Class>>(); for (Map.Entry<Class, TreeEntity> entry : treeEntities.entrySet()) { Class detail = entry.getKey(); for (Class master : entry.getValue().getMasterEntities()) { Set<Class> details = m2dMap.get(master); if (details == null) { m2dMap.put(master, details = new HashSet<Class>()); } details.add(detail); } } return m2dMap; }
From source file:com.github.gekoh.yagen.util.MappingUtils.java
public static Iterator<Class> getClassesSequenceIterator(EntityManagerFactory emf, boolean topDown) throws Exception { Map<Class, TreeEntity> treeEntities = new HashMap<Class, TreeEntity>(); for (EntityType entityType : emf.getMetamodel().getEntities()) { Class entityClass = entityType.getJavaType(); if (!treeEntities.containsKey(entityClass)) { treeEntities.put(entityClass, new TreeEntity(entityClass)); }//www . j a v a 2 s. co m fillTreeEntityMap(treeEntities, entityType.getAttributes(), entityClass); } for (TreeEntity treeEntity : treeEntities.values()) { if (topDown) { treeEntity.calculateMaxLevel(treeEntities); } else { treeEntity.calculateMinLevel(treeEntities); } } List<TreeEntity> sorted = new ArrayList<TreeEntity>(treeEntities.values()); Collections.sort(sorted); if (!topDown) { Collections.reverse(sorted); } List<Class> sortedClasses = new ArrayList<Class>(sorted.size()); for (TreeEntity treeEntity : sorted) { sortedClasses.add(treeEntity.getEntityClass()); } return sortedClasses.iterator(); }
From source file:bq.jpa.demo.metadata.service.MetadataService.java
public void doMD() { Metamodel md = em.getMetamodel();/*from w ww .j a v a 2 s. co m*/ EntityType<Employee> employee = md.entity(Employee.class); for (Attribute<? super Employee, ?> attr : employee.getAttributes()) { System.out.println(attr.getName() + " | " + attr.getJavaType().getName() + " | " + attr.getPersistentAttributeType()); } }
From source file:com.impetus.client.rdbms.query.RDBMSEntityReader.java
/** * Gets the sql query from jpa.//from w w w. j av a 2 s. c o m * * @param entityMetadata * the entity metadata * @param relations * the relations * @param primaryKeys * the primary keys * @return the sql query from jpa */ public String getSqlQueryFromJPA(EntityMetadata entityMetadata, List<String> relations, Set<String> primaryKeys) { ApplicationMetadata appMetadata = kunderaMetadata.getApplicationMetadata(); Metamodel metaModel = appMetadata.getMetamodel(entityMetadata.getPersistenceUnit()); if (jpaQuery != null) { String query = appMetadata.getQuery(jpaQuery); boolean isNative = kunderaQuery != null ? kunderaQuery.isNative() : false; if (isNative) { return query != null ? query : jpaQuery; } } // Suffixing the UNDERSCORE instead of prefix as Oracle 11g complains // about invalid characters error while executing the request. String aliasName = entityMetadata.getTableName() + "_"; StringBuilder queryBuilder = new StringBuilder("Select "); EntityType entityType = metaModel.entity(entityMetadata.getEntityClazz()); Set<Attribute> attributes = entityType.getAttributes(); for (Attribute field : attributes) { if (!field.isAssociation() && !field.isCollection() && !((Field) field.getJavaMember()).isAnnotationPresent(ManyToMany.class) && !((Field) field.getJavaMember()).isAnnotationPresent(Transient.class) && !((MetamodelImpl) metaModel) .isEmbeddable(((AbstractAttribute) field).getBindableJavaType())) { queryBuilder.append(aliasName); queryBuilder.append("."); queryBuilder.append(((AbstractAttribute) field).getJPAColumnName()); queryBuilder.append(", "); } } // Handle embedded columns, add them to list. Map<String, EmbeddableType> embeddedColumns = ((MetamodelImpl) metaModel) .getEmbeddables(entityMetadata.getEntityClazz()); for (EmbeddableType embeddedCol : embeddedColumns.values()) { Set<Attribute> embeddedAttributes = embeddedCol.getAttributes(); for (Attribute column : embeddedAttributes) { queryBuilder.append(aliasName); queryBuilder.append("."); queryBuilder.append(((AbstractAttribute) column).getJPAColumnName()); queryBuilder.append(", "); } } if (relations != null) { for (String relation : relations) { Relation rel = entityMetadata.getRelation(entityMetadata.getFieldName(relation)); String r = MetadataUtils.getMappedName(entityMetadata, rel, kunderaMetadata); if (!((AbstractAttribute) entityMetadata.getIdAttribute()).getJPAColumnName() .equalsIgnoreCase(r != null ? r : relation) && rel != null && !rel.getProperty().isAnnotationPresent(ManyToMany.class) && !rel.getProperty().isAnnotationPresent(OneToMany.class) && (rel.getProperty().isAnnotationPresent(OneToOne.class) && StringUtils.isBlank(rel.getMappedBy()) || rel.getProperty().isAnnotationPresent(ManyToOne.class))) { queryBuilder.append(aliasName); queryBuilder.append("."); queryBuilder.append(r != null ? r : relation); queryBuilder.append(", "); } } } // Remove last "," queryBuilder.deleteCharAt(queryBuilder.lastIndexOf(",")); queryBuilder.append(" From "); if (entityMetadata.getSchema() != null && !entityMetadata.getSchema().isEmpty()) { queryBuilder.append(entityMetadata.getSchema() + "."); } queryBuilder.append(entityMetadata.getTableName()); queryBuilder.append(" "); queryBuilder.append(aliasName); // add conditions if (filter != null) { queryBuilder.append(" Where "); } // Append conditions onCondition(entityMetadata, (MetamodelImpl) metaModel, primaryKeys, aliasName, queryBuilder, entityType); return queryBuilder.toString(); }
From source file:org.jdal.dao.jpa.JpaDao.java
/** * Null References on one to many and one to one associations. * Will only work if association has annotated with a mappedBy attribute. * //from w w w. j av a2 s .c o m * @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); } } } } } }
From source file:com.impetus.client.oraclenosql.OracleNoSQLClient.java
/** * Creates the row.//from w w w. ja va2 s . c o m * * @param entityMetadata * the entity metadata * @param entity * the entity * @param id * the id * @param rlHolders * the rl holders * @return the row */ private Row createRow(EntityMetadata entityMetadata, Object entity, Object id, List<RelationHolder> rlHolders) { String schema = entityMetadata.getSchema(); // Irrelevant for this // datastore String table = entityMetadata.getTableName(); MetamodelImpl metamodel = (MetamodelImpl) KunderaMetadataManager.getMetamodel(kunderaMetadata, entityMetadata.getPersistenceUnit()); Table schemaTable = null; try { schemaTable = tableAPI.getTable(table); if (schemaTable == null) { log.error("No table found for " + table); throw new KunderaException("No table found for " + table); } } catch (FaultException ex) { log.error("Error while getting table " + table + ". Caused By: ", ex); throw new KunderaException("Error while getting table " + table + ". Caused By: ", ex); } Row row = schemaTable.createRow(); if (log.isDebugEnabled()) { log.debug("Persisting data into " + schema + "." + table + " for " + id); } EntityType entityType = metamodel.entity(entityMetadata.getEntityClazz()); Set<Attribute> attributes = entityType.getAttributes(); // process entity attributes. process(entity, metamodel, row, attributes, schemaTable, entityMetadata); // on relational attributes. onRelationalAttributes(rlHolders, row, schemaTable); // add discriminator column(if present) addDiscriminatorColumn(row, entityType, schemaTable); return row; }