private void analyzeAttributes(ManagedType<?> managedType, String targetTableName) { managedType.getSingularAttributes().forEach(attribute -> { ManagedType<?> targetEntity = managedTypesByClass.get(attribute.getJavaType()); if (targetEntity != null && attribute.getType() instanceof EmbeddableType) { analyzeAttributes((EmbeddableType) attribute.getType(), targetTableName); } else if (targetEntity != null && attribute.getType() instanceof IdentifiableType) { // this is a relation Map<String, String> referenceColumnNamesMap = getReferenceColumnNamesMapForReferenceAttribute(attribute, targetEntity); singularAttributesByForeignKeyRelation.put( new ForeignKeyRelation(targetTableName, getTableName(targetEntity.getJavaType()), referenceColumnNamesMap), attribute ); } else { String columnName = getSingularAttributeColumnName(attribute); singularAttributesByColumnName.put(new QualifiedColumnName(targetTableName, columnName), attribute); } }); }
@SuppressWarnings("rawtypes") @Override public EmbeddableType<?> getJPAEmbeddableType() { @SuppressWarnings("hiding") class JPAComplexAttribute<Long> extends JPAEmbeddableMock<Long> { @SuppressWarnings("unchecked") @Override public Class<Long> getJavaType() { return (Class<Long>) java.lang.Long.class; } } return new JPAComplexAttribute(); }
public QEntity getEmbeddable(final Class clazz, final EmbeddableType ct) { if (!entities.containsKey(clazz)) { if (log.isDebugEnabled()) log.debug("Begin create QEntity " + clazz + " from EmbeddableType " + ct); QEntity entity = new QEntity(clazz); entities.put(clazz, entity); entity.parseEmbeddable(this, sessionFactory, null, ct); if (log.isDebugEnabled()) log.debug("End create QEntity " + clazz + " from EmbeddableType " + ct); } return entities.get(clazz); }
private static Set<Class> toTypes(EntityManagerFactory entityManagerFactory) { Set<Class> set = new HashSet<>(); Set<EmbeddableType<?>> embeddables = entityManagerFactory.getMetamodel().getEmbeddables(); for (EmbeddableType<?> embeddable : embeddables) { set.add(embeddable.getJavaType()); } Set<EntityType<?>> entities = entityManagerFactory.getMetamodel().getEntities(); for (EntityType<?> entity : entities) { set.add(entity.getJavaType()); } return set; }
@Override public void discoverElements(MetaProviderContext context) { if (entityManagerFactory != null) { Set<EmbeddableType<?>> embeddables = entityManagerFactory.getMetamodel().getEmbeddables(); for (EmbeddableType<?> embeddable : embeddables) { context.getLookup().getMeta(embeddable.getJavaType(), MetaJpaDataObject.class); } Set<EntityType<?>> entities = entityManagerFactory.getMetamodel().getEntities(); for (EntityType<?> entity : entities) { context.getLookup().getMeta(entity.getJavaType(), MetaJpaDataObject.class); } } }
/** * Gets the attribute names for the primary keys from the given entity type. * * @param entityType The entity type of the data object. * @return A list of primary key attribute names. */ protected List<String> getPrimaryKeyAttributeNames(EntityType<?> entityType) { List<String> primaryKeyAttributeNames = new ArrayList<String>(); // JHK: After examining of the metadata structures of EclipseLink, I determined that there // was nothing in those which preserved the order of the original annotations. // We *need* to know the order of PK fields for KNS/KRAD functionality. // So, I'm falling back to checking the annotations and fields on the referenced objects. // Yes, the Javadoc states that the getDeclaredFields() method does not guarantee order, // But, it's the best we have. And, as of Java 6, it is returning them in declaration order. if (entityType.getIdType() instanceof EmbeddableType) { for (Field pkField : entityType.getIdType().getJavaType().getDeclaredFields()) { primaryKeyAttributeNames.add(pkField.getName()); } } else { // First, get the ID attributes from the metadata List<String> unsortedPkFields = new ArrayList<String>(); for (SingularAttribute attr : entityType.getSingularAttributes()) { if (attr.isId()) { unsortedPkFields.add(attr.getName()); } } getPrimaryKeyNamesInOrder(primaryKeyAttributeNames, unsortedPkFields, entityType.getJavaType().getDeclaredFields(), entityType.getJavaType()); } return primaryKeyAttributeNames; }
public JPAEdmComplexType(final JPAEdmSchemaView view, final Attribute<?, ?> complexAttribute) { super(view); schemaView = view; for (EmbeddableType<?> jpaEmbeddable : schemaView.getJPAMetaModel().getEmbeddables()) { if (jpaEmbeddable.getJavaType().getName().equals(complexAttribute.getJavaType().getName())) { nestedComplexType = jpaEmbeddable; break; } } directBuild = false; if (nonKeyComplexList == null) { nonKeyComplexList = new ArrayList<String>(); } }
/** * Parse an @Embeddable * * @param entityFactory * @param sessionFactory * @param prefix * @param type */ public void parseEmbeddable(final QEntityFactory entityFactory, final SessionFactoryImplementor sessionFactory, final String prefix, final EmbeddableType<?> type) { this.metamodelEmbeddable = type; // Make sure the entity factory sees this embeddable entityFactory.getEmbeddable(type.getJavaType(), type); for (Attribute<?, ?> attribute : type.getAttributes()) { parseFields(entityFactory, sessionFactory, prefix, attribute); } }
public JPAEdmComplexType(final JPAEdmSchemaView view, final Attribute<?, ?> complexAttribute) { super(view); schemaView = view; for (EmbeddableType<?> jpaEmbeddable : schemaView.getJPAMetaModel().getEmbeddables()) { if (jpaEmbeddable.getJavaType().getName().equals(complexAttribute.getJavaType().getName())) { nestedComplexType = jpaEmbeddable; break; } } directBuild = false; }
@Override public EmbeddableType<?> getJPAEmbeddableType() { return currentEmbeddableType; }
@Override public void build() throws ODataJPAModelException, ODataJPARuntimeException { Set<EmbeddableType<?>> embeddables = new HashSet<EmbeddableType<?>>(); if (consistentComplextTypes == null) { consistentComplextTypes = new ArrayList<ComplexType>(); } if (searchMap == null) { searchMap = new HashMap<String, ComplexType>(); } if (directBuild) { embeddables = schemaView.getJPAMetaModel().getEmbeddables(); } else { embeddables.add(nestedComplexType); } for (EmbeddableType<?> embeddableType : embeddables) { currentEmbeddableType = embeddableType; String searchKey = embeddableType.getJavaType().getName(); if (searchMap.containsKey(searchKey)) { continue; } // Check for need to Exclude if (isExcluded(JPAEdmComplexType.this)) { continue; } JPAEdmPropertyView propertyView = new JPAEdmProperty(schemaView, JPAEdmComplexType.this); propertyView.getBuilder().build(); currentComplexType = new ComplexType(); currentComplexType.setProperties(propertyView.getEdmPropertyList()); JPAEdmNameBuilder.build(JPAEdmComplexType.this); searchMap.put(searchKey, currentComplexType); consistentComplextTypes.add(currentComplexType); } }
@Override public <X> EmbeddableType<X> embeddable(final Class<X> arg0) { return null; }
@Override public Set<EmbeddableType<?>> getEmbeddables() { return null; }
@Override public EmbeddableType<?> getJPAEmbeddableType() { return null; }
public JPAEdmMetaModel() { embeddableSet = new HashSet<EmbeddableType<?>>(); }
@Override public Set<EmbeddableType<?>> getEmbeddables() { embeddableSet.add(new JPAEdmEmbeddable<String>()); return embeddableSet; }
@Override public EmbeddableType<?> getJPAEmbeddableType() { return new JPAEdmEmbeddable<java.lang.String>(); }
public JPAEdmMetaModel() { entities = new HashSet<EntityType<?>>(); embeddableSet = new HashSet<EmbeddableType<?>>(); }
/** * Adds an embeddable type definition * * @param sattr - The embeddable type metamodel */ String addComponent(SingularAttribute sattr) { Class<?> type = sattr.getJavaType(); // "Location:#com.breeze.model" String classKey = getEntityTypeName(type); if (_typeNames.contains(classKey)) { // Only add a complex type definition once. return classKey; } HashMap<String, Object> cmap = new LinkedHashMap<String, Object>(); _typeList.add(0, cmap); _typeNames.add(classKey); cmap.put("shortName", type.getSimpleName()); cmap.put("namespace", type.getPackage().getName()); cmap.put("isComplexType", true); ArrayList<HashMap<String, Object>> dataArrayList = new ArrayList<HashMap<String, Object>>(); cmap.put("dataProperties", dataArrayList); EmbeddableType<?> bed = _emFactory.getMetamodel().embeddable(type); for (Attribute<?,?> attrib : bed.getAttributes()) { PersistentAttributeType attrType = attrib.getPersistentAttributeType(); if (!(attrib instanceof SingularAttribute)) { throw new RuntimeException("Collections not supported in complex types"); } SingularAttribute cattr = (SingularAttribute) attrib; if (attrType == PersistentAttributeType.EMBEDDED) { // nested complex type String complexTypeName = addComponent(cattr); HashMap<String, Object> compMap = new HashMap<String, Object>(); compMap.put("nameOnServer", attrib.getName()); compMap.put("complexTypeName", complexTypeName); compMap.put("isNullable", cattr.isOptional()); dataArrayList.add(compMap); } else { // data property HashMap<String, Object> dmap = makeDataProperty(cattr.getName(), cattr, false, false); dataArrayList.add(dmap); } } return classKey; }
/** * Parse an @Entity * * @param entityFactory * @param metadata * @param sessionFactory */ void parse(QEntityFactory entityFactory, EntityType<?> metadata, SessionFactoryImplementor sessionFactory) { this.metamodelEntity = metadata; this.name = metadata.getName(); for (Attribute<?, ?> attribute : metadata.getAttributes()) { parseFields(entityFactory, sessionFactory, null, attribute); } // Parse top-level properties // Add identifier property { if (!metadata.hasSingleIdAttribute()) throw new IllegalArgumentException("@IdClass Entity not supported! " + metadata.getJavaType()); Type idType = metadata.getIdType(); switch (idType.getPersistenceType()) { case BASIC: break; // No action necessary, will be processed like a normal field case EMBEDDABLE: { EmbeddableType<?> emb = (EmbeddableType<?>) idType; parseEmbeddable(entityFactory, sessionFactory, "id", emb); break; } default: throw new IllegalArgumentException("Cannot handle id type: " + idType.getPersistenceType() + ": " + idType); } } // Add links to descendants { final List<QEntity> descendants = entityFactory.getSubclasses(clazz); if (!descendants.isEmpty()) this.descendants = descendants; } // Figure out the id method/field final String idPropertyName = getIdPropertyName(); if (idPropertyName != null) this.idProperty = new PropertyWrapper(clazz, idPropertyName); }
static <T> EmbeddableType<T> getEmbeddableType(Bindable<T> attribute, Metamodel metamodel) { logger.debug("getEmbeddableType({},{})", attribute, metamodel); EmbeddableType<T> ret = metamodel.embeddable(attribute.getBindableJavaType()); logger.debug("getEmbeddableType -> {}", ret); return ret; }
@Override public void build() throws ODataJPAModelException, ODataJPARuntimeException { Set<EmbeddableType<?>> embeddables = new HashSet<EmbeddableType<?>>(); if (consistentComplextTypes == null) { consistentComplextTypes = new ArrayList<ComplexType>(); } if (searchMap == null) { searchMap = new HashMap<String, ComplexType>(); } if (directBuild) { embeddables = schemaView.getJPAMetaModel().getEmbeddables(); } else { embeddables.add(nestedComplexType); } for (EmbeddableType<?> embeddableType : embeddables) { currentEmbeddableType = embeddableType; String searchKey = embeddableType.getJavaType().getName(); if (searchMap.containsKey(searchKey)) { continue; } // Check for need to Exclude if (isExcluded(JPAEdmComplexType.this)) { continue; } JPAEdmPropertyView propertyView = new JPAEdmProperty( schemaView, JPAEdmComplexType.this); propertyView.getBuilder().build(); currentComplexType = new ComplexType(); currentComplexType .setProperties(propertyView.getEdmPropertyList()); JPAEdmNameBuilder.build(JPAEdmComplexType.this); searchMap.put(searchKey, currentComplexType); consistentComplextTypes.add(currentComplexType); } }