/** * Get a relationship type. * * @param relation Name of relationship * @return Relationship type. RelationshipType.NONE if not found. */ public RelationshipType getRelationshipType(String relation) { return dictionary.getRelationshipType(obj, relation); }
/** * Get a relationship type. * * @param relation Name of relationship * @return Relationship type. RelationshipType.NONE if not found. */ public RelationshipType getRelationshipType(String relation) { return dictionary.getRelationshipType(obj, relation); }
/** * Get the type of relationship from a relation. * * @param entity Entity instance * @param relation Name of relationship field * @return Relationship type. RelationshipType.NONE if is none found. */ public RelationshipType getRelationshipType(Object entity, String relation) { return getRelationshipType(entity.getClass(), relation); }
/** * Get the type of relationship from a relation. * * @param entity Entity instance * @param relation Name of relationship field * @return Relationship type. RelationshipType.NONE if is none found. */ public RelationshipType getRelationshipType(Object entity, String relation) { return getRelationshipType(entity.getClass(), relation); }
public static boolean toManyInPath(EntityDictionary dictionary, Path path) { return path.getPathElements().stream() .map(element -> dictionary.getRelationshipType(element.getType(), element.getFieldName())) .anyMatch(RelationshipType::isToMany); }
public static boolean toManyInPath(EntityDictionary dictionary, Path path) { return path.getPathElements().stream() .map(element -> dictionary.getRelationshipType(element.getType(), element.getFieldName())) .anyMatch(RelationshipType::isToMany); }
/** * Extract the relationships of the entity. */ private void setRelationships() { if (this.data != null) { this.relationships = new LinkedHashSet<>(); EntityDictionary dictionary = this.requestScope.getDictionary(); for (Map.Entry<String, Object> entry : this.data.entrySet()) { if (dictionary.isRelation(this.entityClass, entry.getKey())) { Set<Entity> entitySet = new LinkedHashSet<>(); Class<?> loadClass = dictionary.getParameterizedType(this.entityClass, entry.getKey()); Boolean isToOne = dictionary.getRelationshipType(this.entityClass, entry.getKey()).isToOne(); if (isToOne) { entitySet.add(new Entity(Optional.of(this), ((Map<String, Object>) entry.getValue()), loadClass, this.requestScope)); } else { for (Map<String, Object> row : (List<Map<String, Object>>) entry.getValue()) { entitySet.add(new Entity(Optional.of(this), row, loadClass, this.requestScope)); } } this.relationships.add(new Relationship(entry.getKey(), entitySet)); } } } }
@Override public Object getRelation(DataStoreTransaction relationTx, Object entity, String relationName, Optional<FilterExpression> filterExpression, Optional<Sorting> sorting, Optional<Pagination> pagination, RequestScope scope) { Object values = PersistentResource.getValue(entity, relationName, scope); // Gather list of valid id's from this parent Map<String, Object> idToChildResource = new HashMap<>(); if (dictionary.getRelationshipType(entity, relationName).isToOne()) { if (values == null) { return null; } idToChildResource.put(dictionary.getId(values), values); } else if (values instanceof Collection) { idToChildResource.putAll((Map) ((Collection) values).stream() .collect(Collectors.toMap(dictionary::getId, Function.identity()))); } else { throw new IllegalStateException("An unexpected error occurred querying a relationship"); } Class entityClass = dictionary.getParameterizedType(entity, relationName); return processData(entityClass, idToChildResource, filterExpression, sorting, pagination, scope); }
/** * Builds a JOIN clause that eagerly fetches to-one relationships that Hibernate needs to hydrate. * @param entityClass The entity class that is being queried in the HQL query. * @param alias The HQL alias for the entity class. * @return The JOIN clause that can be added to the FROM clause. */ protected String extractToOneMergeJoins(Class<?> entityClass, String alias) { List<String> relationshipNames = dictionary.getRelationships(entityClass); StringBuilder joinString = new StringBuilder(""); for (String relationshipName : relationshipNames) { RelationshipType type = dictionary.getRelationshipType(entityClass, relationshipName); if (type.isToOne() && !type.isComputed()) { // fetch only OneToOne with mappedBy OneToOne oneToOne = dictionary.getAttributeOrRelationAnnotation( entityClass, OneToOne.class, relationshipName); if (oneToOne == null || oneToOne.mappedBy().isEmpty()) { continue; } joinString.append(" LEFT JOIN FETCH "); joinString.append(alias); joinString.append(PERIOD); joinString.append(relationshipName); joinString.append(SPACE); } } return joinString.toString(); }
/** * Extract the relationships of the entity. */ private void setRelationships() { if (this.data != null) { this.relationships = new LinkedHashSet<>(); EntityDictionary dictionary = this.requestScope.getDictionary(); for (Map.Entry<String, Object> entry : this.data.entrySet()) { if (dictionary.isRelation(this.entityClass, entry.getKey())) { Set<Entity> entitySet = new LinkedHashSet<>(); Class<?> loadClass = dictionary.getParameterizedType(this.entityClass, entry.getKey()); Boolean isToOne = dictionary.getRelationshipType(this.entityClass, entry.getKey()).isToOne(); if (isToOne) { entitySet.add(new Entity(Optional.of(this), ((Map<String, Object>) entry.getValue()), loadClass, this.requestScope)); } else { for (Map<String, Object> row : (List<Map<String, Object>>) entry.getValue()) { entitySet.add(new Entity(Optional.of(this), row, loadClass, this.requestScope)); } } this.relationships.add(new Relationship(entry.getKey(), entitySet)); } } } }
@Override public Object getRelation(DataStoreTransaction relationTx, Object entity, String relationName, Optional<FilterExpression> filterExpression, Optional<Sorting> sorting, Optional<Pagination> pagination, RequestScope scope) { Object values = PersistentResource.getValue(entity, relationName, scope); // Gather list of valid id's from this parent Map<String, Object> idToChildResource = new HashMap<>(); if (dictionary.getRelationshipType(entity, relationName).isToOne()) { if (values == null) { return null; } idToChildResource.put(dictionary.getId(values), values); } else if (values instanceof Collection) { idToChildResource.putAll((Map) ((Collection) values).stream() .collect(Collectors.toMap(dictionary::getId, Function.identity()))); } else { throw new IllegalStateException("An unexpected error occurred querying a relationship"); } Class entityClass = dictionary.getParameterizedType(entity, relationName); return processData(entityClass, idToChildResource, filterExpression, sorting, pagination, scope); }
RelationshipType type = dictionary.getRelationshipType(clazz, relationship);
@Override public Object getRelation(DataStoreTransaction relationTx, Object entity, String relationName, Optional<FilterExpression> filter, Optional<Sorting> sorting, Optional<Pagination> pagination, RequestScope scope) { relationTx = getRelationTransaction(entity, relationName); DataStoreTransaction entityTransaction = getTransaction(entity.getClass()); EntityDictionary dictionary = scope.getDictionary(); Class<?> relationClass = dictionary.getParameterizedType(entity, relationName); String idFieldName = dictionary.getIdFieldName(relationClass); // If different transactions, check if bridgeable and try to bridge if (entityTransaction != relationTx && relationTx instanceof BridgeableTransaction) { BridgeableTransaction bridgeableTx = (BridgeableTransaction) relationTx; RelationshipType relationType = dictionary.getRelationshipType(entity.getClass(), relationName); Serializable id = filter.map(fe -> extractId(fe, idFieldName, relationClass)).orElse(null); if (relationType.isToMany()) { return id == null ? bridgeableTx.bridgeableLoadObjects( this, entity, relationName, filter, sorting, pagination, scope) : bridgeableTx.bridgeableLoadObject(this, entity, relationName, id, filter, scope); } return bridgeableTx.bridgeableLoadObject(this, entity, relationName, id, filter, scope); } // Otherwise, rely on existing underlying transaction to call correctly into relationTx return entityTransaction.getRelation(relationTx, entity, relationName, filter, sorting, pagination, scope); }
RelationshipType type = dictionary.getRelationshipType(clazz, relationship);
/** * Validates that none of the provided path's relationships are to-many * @param path The path to validate * @param dictionary The elide entity dictionary * @return True if the path is valid. False otherwise. */ protected static boolean isValidSortRulePath(Path path, EntityDictionary dictionary) { //Validate that none of the relationships are to-many for (Path.PathElement pathElement : path.getPathElements()) { if (! dictionary.isRelation(pathElement.getType(), pathElement.getFieldName())) { continue; } if (dictionary.getRelationshipType(pathElement.getType(), pathElement.getFieldName()).isToMany()) { return false; } } return true; }
/** * Validates that none of the provided path's relationships are to-many * @param path The path to validate * @param dictionary The elide entity dictionary * @return True if the path is valid. False otherwise. */ protected static boolean isValidSortRulePath(Path path, EntityDictionary dictionary) { //Validate that none of the relationships are to-many for (Path.PathElement pathElement : path.getPathElements()) { if (! dictionary.isRelation(pathElement.getType(), pathElement.getFieldName())) { continue; } if (dictionary.getRelationshipType(pathElement.getType(), pathElement.getFieldName()).isToMany()) { return false; } } return true; }
String entityName; try { RelationshipType type = dictionary.getRelationshipType(resource.getObject(), subCollection); if (type == RelationshipType.NONE) { throw new InvalidCollectionException(subCollection);
String entityName; try { RelationshipType type = dictionary.getRelationshipType(resource.getObject(), subCollection); if (type == RelationshipType.NONE) { throw new InvalidCollectionException(subCollection);
RelationshipType type = dictionary.getRelationshipType(entityClass, relationship);
RelationshipType relationshipType = dictionary.getRelationshipType(parentClass, name);