use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.
the class QueryModelSelectionVisitor method visit.
@Override
public void visit(IdExpression<?, ?> idExpression) {
PersistentEntityRoot<?> root = idExpression.getRoot();
PersistentEntity persistentEntity = root.getPersistentEntity();
if (persistentEntity.hasCompositeIdentity()) {
for (PersistentProperty persistentProperty : persistentEntity.getCompositeIdentity()) {
if (distinct) {
addProjection(Projections.distinct(persistentProperty.getName()));
} else {
addProjection(Projections.property(persistentProperty.getName()));
}
}
} else {
PersistentProperty identity = persistentEntity.getIdentity();
if (distinct) {
addProjection(Projections.distinct(identity.getName()));
} else {
addProjection(Projections.property(identity.getName()));
}
}
}
use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.
the class AbstractSqlRepositoryOperations method idPropertiesWithValues.
private Stream<Map.Entry<PersistentProperty, Object>> idPropertiesWithValues(PersistentProperty property, Object value) {
Object propertyValue = ((RuntimePersistentProperty) property).getProperty().get(value);
if (property instanceof Embedded) {
Embedded embedded = (Embedded) property;
PersistentEntity embeddedEntity = embedded.getAssociatedEntity();
return embeddedEntity.getPersistentProperties().stream().flatMap(prop -> idPropertiesWithValues(prop, propertyValue));
} else if (property instanceof Association) {
Association association = (Association) property;
if (association.isForeignKey()) {
return Stream.empty();
}
PersistentEntity associatedEntity = association.getAssociatedEntity();
PersistentProperty identity = associatedEntity.getIdentity();
if (identity == null) {
throw new IllegalStateException("Identity cannot be missing for: " + associatedEntity);
}
return idPropertiesWithValues(identity, propertyValue);
}
return Stream.of(new AbstractMap.SimpleEntry<>(property, propertyValue));
}
use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.
the class MongoQueryBuilder method addLookups.
private void addLookups(Collection<JoinPath> joins, QueryState queryState) {
if (joins.isEmpty()) {
return;
}
List<String> joined = joins.stream().map(JoinPath::getPath).sorted((o1, o2) -> Comparator.comparingInt(String::length).thenComparing(String::compareTo).compare(o1, o2)).collect(Collectors.toList());
for (String join : joined) {
StringJoiner rootPath = new StringJoiner(".");
StringJoiner currentEntityPath = new StringJoiner(".");
LookupsStage currentLookup = queryState.rootLookups;
for (String path : StringUtils.splitOmitEmptyStrings(join, '.')) {
rootPath.add(path);
currentEntityPath.add(path);
String thisPath = currentEntityPath.toString();
if (currentLookup.subLookups.containsKey(thisPath)) {
currentLookup = currentLookup.subLookups.get(path);
currentEntityPath = new StringJoiner(".");
continue;
}
PersistentPropertyPath propertyPath = currentLookup.persistentEntity.getPropertyPath(thisPath);
PersistentProperty property = propertyPath.getProperty();
if (!(property instanceof Association)) {
continue;
}
Association association = (Association) property;
if (association.getKind() == Relation.Kind.EMBEDDED) {
continue;
}
LookupsStage lookupStage = new LookupsStage(association.getAssociatedEntity());
List<Map<String, Object>> pipeline = currentLookup.pipeline;
Optional<Association> inverseSide = association.getInverseSide().map(Function.identity());
PersistentEntity persistentEntity = association.getOwner();
String joinedCollectionName = association.getAssociatedEntity().getPersistedName();
String ownerCollectionName = persistentEntity.getPersistedName();
if (association.getKind() == Relation.Kind.MANY_TO_MANY || association.isForeignKey() && !inverseSide.isPresent()) {
PersistentEntity associatedEntity = association.getAssociatedEntity();
PersistentEntity associationOwner = association.getOwner();
// JOIN TABLE
PersistentProperty identity = associatedEntity.getIdentity();
if (identity == null) {
throw new IllegalArgumentException("Associated entity [" + associatedEntity.getName() + "] defines no ID. Cannot join.");
}
final PersistentProperty associatedId = associationOwner.getIdentity();
if (associatedId == null) {
throw new MappingException("Cannot join on entity [" + associationOwner.getName() + "] that has no declared ID");
}
Association owningAssociation = inverseSide.orElse(association);
boolean isAssociationOwner = !association.getInverseSide().isPresent();
NamingStrategy namingStrategy = associationOwner.getNamingStrategy();
AnnotationMetadata annotationMetadata = owningAssociation.getAnnotationMetadata();
List<String> ownerJoinFields = resolveJoinTableAssociatedFields(annotationMetadata, isAssociationOwner, associationOwner, namingStrategy);
List<String> ownerJoinCollectionFields = resolveJoinTableJoinFields(annotationMetadata, isAssociationOwner, associationOwner, namingStrategy);
List<String> associationJoinFields = resolveJoinTableAssociatedFields(annotationMetadata, !isAssociationOwner, associatedEntity, namingStrategy);
List<String> associationJoinCollectionFields = resolveJoinTableJoinFields(annotationMetadata, !isAssociationOwner, associatedEntity, namingStrategy);
String joinCollectionName = namingStrategy.mappedName(owningAssociation);
// String joinTableName = annotationMetadata
// .stringValue(ANN_JOIN_TABLE, "name")
// .orElseGet(() -> namingStrategy.mappedName(association));
List<Map<String, Object>> joinCollectionLookupPipeline = new ArrayList<>();
pipeline.add(lookup(joinCollectionName, "_id", ownerCollectionName, joinCollectionLookupPipeline, thisPath));
joinCollectionLookupPipeline.add(lookup(joinedCollectionName, joinedCollectionName, "_id", lookupStage.pipeline, joinedCollectionName));
joinCollectionLookupPipeline.add(unwind("$" + joinedCollectionName, true));
joinCollectionLookupPipeline.add(singletonMap("$replaceRoot", singletonMap("newRoot", "$" + joinedCollectionName)));
} else {
String currentPath = asPath(propertyPath.getAssociations(), propertyPath.getProperty());
if (association.isForeignKey()) {
String mappedBy = association.getAnnotationMetadata().stringValue(Relation.class, "mappedBy").orElseThrow(IllegalStateException::new);
PersistentPropertyPath mappedByPath = association.getAssociatedEntity().getPropertyPath(mappedBy);
if (mappedByPath == null) {
throw new IllegalStateException("Cannot find mapped path: " + mappedBy);
}
if (!(mappedByPath.getProperty() instanceof Association)) {
throw new IllegalStateException("Expected association as a mapped path: " + mappedBy);
}
List<String> localMatchFields = new ArrayList<>();
List<String> foreignMatchFields = new ArrayList<>();
traversePersistentProperties(currentLookup.persistentEntity.getIdentity(), (associations, p) -> {
String fieldPath = asPath(associations, p);
localMatchFields.add(fieldPath);
});
List<Association> mappedAssociations = new ArrayList<>(mappedByPath.getAssociations());
mappedAssociations.add((Association) mappedByPath.getProperty());
traversePersistentProperties(mappedAssociations, currentLookup.persistentEntity.getIdentity(), (associations, p) -> {
String fieldPath = asPath(associations, p);
foreignMatchFields.add(fieldPath);
});
pipeline.add(lookup(joinedCollectionName, localMatchFields, foreignMatchFields, lookupStage.pipeline, currentPath));
} else {
List<Association> mappedAssociations = new ArrayList<>(propertyPath.getAssociations());
mappedAssociations.add((Association) propertyPath.getProperty());
List<String> localMatchFields = new ArrayList<>();
List<String> foreignMatchFields = new ArrayList<>();
PersistentProperty identity = lookupStage.persistentEntity.getIdentity();
if (identity == null) {
throw new IllegalStateException("Null identity of persistent entity: " + lookupStage.persistentEntity);
}
traversePersistentProperties(mappedAssociations, identity, (associations, p) -> {
String fieldPath = asPath(associations, p);
localMatchFields.add(fieldPath);
});
traversePersistentProperties(identity, (associations, p) -> {
String fieldPath = asPath(associations, p);
foreignMatchFields.add(fieldPath);
});
pipeline.add(lookup(joinedCollectionName, localMatchFields, foreignMatchFields, lookupStage.pipeline, currentPath));
}
if (association.getKind().isSingleEnded()) {
pipeline.add(unwind("$" + currentPath, true));
}
}
currentLookup.subLookups.put(currentEntityPath.toString(), lookupStage);
}
queryState.joinPaths.add(join);
}
}
use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.
the class MongoReactiveCollectionsCreator method initialize.
@PostConstruct
void initialize(BeanLocator beanLocator, RuntimeEntityRegistry runtimeEntityRegistry, List<AbstractMongoConfiguration> mongoConfigurations) {
super.initialize(runtimeEntityRegistry, mongoConfigurations, mongoConfiguration -> {
ReactiveMongoDatabaseFactory mongoDatabaseFactory = getMongoFactory(ReactiveMongoDatabaseFactory.class, beanLocator, mongoConfiguration);
Map<String, Set<String>> databaseCollections = new HashMap<>();
return new DatabaseOperations<MongoDatabase>() {
@Override
public String getDatabaseName(MongoDatabase database) {
return database.getName();
}
@Override
public MongoDatabase find(PersistentEntity persistentEntity) {
return mongoDatabaseFactory.getDatabase(persistentEntity);
}
@Override
public Set<String> listCollectionNames(MongoDatabase database) {
return databaseCollections.computeIfAbsent(database.getName(), s -> Flux.from(database.listCollectionNames()).collect(Collectors.toSet()).block());
}
@Override
public void createCollection(MongoDatabase database, String collection) {
Mono.from(database.createCollection(collection)).block();
}
};
});
}
use of io.micronaut.data.model.PersistentEntity in project micronaut-data by micronaut-projects.
the class SqlQueryBuilder method findOwner.
private Optional<PersistentEntity> findOwner(List<Association> associations, PersistentProperty property) {
PersistentEntity owner = property.getOwner();
if (!owner.isEmbeddable()) {
return Optional.of(owner);
}
ListIterator<Association> listIterator = associations.listIterator(associations.size());
while (listIterator.hasPrevious()) {
Association association = listIterator.previous();
if (!association.getOwner().isEmbeddable()) {
return Optional.of(association.getOwner());
}
}
return Optional.empty();
}
Aggregations