Skip to content

Commit

Permalink
Merge pull request #50 from kaiso/B#49
Browse files Browse the repository at this point in the history
B#49
  • Loading branch information
kaiso authored May 27, 2020
2 parents 97c3da7 + 61beb19 commit 8807992
Show file tree
Hide file tree
Showing 22 changed files with 1,080 additions and 103 deletions.
28 changes: 20 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.github.kaiso.relmongo</groupId>
<artifactId>relmongo</artifactId>
<version>3.2.0</version>
<version>3.3.3</version>
<packaging>jar</packaging>

<name>relmongo</name>
Expand All @@ -18,8 +18,8 @@
<maven.compiler.target>1.8</maven.compiler.target>
<maven.plugin.source.version>3.0.1</maven.plugin.source.version>
<maven.plugin.javadoc.version>3.0.1</maven.plugin.javadoc.version>
<spring.data.version>2.2.1.RELEASE</spring.data.version>
<spring.version>5.2.1.RELEASE</spring.version>
<spring.data.version>2.2.6.RELEASE</spring.data.version>
<spring.version>5.2.5.RELEASE</spring.version>
<mongo.driver.version>3.11.2</mongo.driver.version>
<junit.jupiter.version>5.4.1</junit.jupiter.version>
<junit.platform.version>1.4.2</junit.platform.version>
Expand Down Expand Up @@ -64,14 +64,14 @@
<version>${spring.data.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
Expand All @@ -81,9 +81,9 @@


<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-async</artifactId>
<version>${mongo.driver.version}</version>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-async</artifactId>
<version>${mongo.driver.version}</version>
</dependency>

<!-- test dependencies -->
Expand Down Expand Up @@ -146,6 +146,18 @@
<version>2.11.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.7.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.4</version>
<scope>test</scope>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
@Target(ElementType.FIELD)
public @interface JoinProperty {
/**
* the name of the attribute that holds the reference to the associated object
* the name of the attribute that holds the reference to the associated object
* @return name
*/
String name() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package io.github.kaiso.relmongo.annotation;

import org.springframework.data.annotation.Reference;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand All @@ -32,7 +30,7 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Reference
@RelMongoAnnotation
public @interface ManyToOne {
/**
* (Optional) Whether the association should be lazily loaded or must be eagerly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package io.github.kaiso.relmongo.annotation;

import org.springframework.data.annotation.Reference;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand All @@ -32,9 +30,9 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Reference
@RelMongoAnnotation
public @interface OneToMany {

/**
* (Optional) Whether the association should be lazily loaded or must be eagerly
* fetched.
Expand All @@ -51,6 +49,7 @@
* (Optional) Whether to apply the remove operation to documents that have been
* removed from the
* relationship and to cascade the remove operation to those documents.
*
* @since 2.3.0
*/
boolean orphanRemoval() default false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package io.github.kaiso.relmongo.annotation;

import org.springframework.data.annotation.Reference;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand All @@ -32,7 +30,7 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Reference
@RelMongoAnnotation
public @interface OneToOne {
/**
* (Optional) Whether the association should be lazily loaded or must be eagerly
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.kaiso.relmongo.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(value = { ElementType.ANNOTATION_TYPE })
public @interface RelMongoAnnotation {

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(value = { ElementType.TYPE })
@Import(value = PersistenceConfiguration.class)
@Import(value = RelMongoConfiguration.class)
public @interface EnableRelMongo {

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* @deprecated use {@link EnableRelMongo} instead
* @deprecated use {@link EnableRelMongo} instead
* @author Kais OMRI
*
*/
@Deprecated
@Retention(RetentionPolicy.RUNTIME)
@Target(value = { ElementType.TYPE })
@Import(value = PersistenceConfiguration.class)
@Import(value = RelMongoConfiguration.class)
public @interface EnableRelationalMongo {

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@

/**
* Copyright 2018 Kais OMRI and authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.github.kaiso.relmongo.config;

import io.github.kaiso.relmongo.events.processor.RelMongoProcessor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;

import java.util.concurrent.CompletableFuture;

/**
*
* @author Kais OMRI (kaiso)
*
*/
@Configuration
public class RelMongoConfiguration {

private static final String LINE_SEPARATOR = System.lineSeparator();

private static final Logger logger = LoggerFactory.getLogger(RelMongoConfiguration.class);

private final ApplicationContext context;

public RelMongoConfiguration(ApplicationContext context) {
super();
this.context = context;
this.setUp();
}

@Bean
public RelMongoProcessor mongoEventListener(MongoOperations mongoOperations) {
return new RelMongoProcessor(mongoOperations);
}


public void setUp() {
CompletableFuture.runAsync(() -> {
if (context.getBeanNamesForType(MongoTemplate.class).length > 0) {
MongoTemplate template = context.getBean(MongoTemplate.class);
MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext = template.getConverter().getMappingContext();
if (mappingContext instanceof RelMongoMappingContext) {
RelMongoMappingContext relMongoMappingContext = (RelMongoMappingContext) mappingContext;
if (relMongoMappingContext.isAutoIndexCreation()) {
logger.warn(
"Index auto creation is deprecated and will be disabled by default in the next releases {} applications should manually set up indexes to avoid compatibility problems and performance issues",
LINE_SEPARATOR);

RelMongoPersistentEntityIndexResolver indexResolver = new RelMongoPersistentEntityIndexResolver(relMongoMappingContext);
RelMongoPersistentEntityIndexCreator relMongoPersistentEntityIndexCreator = new RelMongoPersistentEntityIndexCreator(
relMongoMappingContext,
template,
indexResolver);

((GenericApplicationContext) context).registerBean("relMongoPersistentEntityIndexCreator",
RelMongoPersistentEntityIndexCreator.class,
() -> relMongoPersistentEntityIndexCreator,
bd -> bd.setLazyInit(false));

((ApplicationEventPublisherAware) mappingContext).setApplicationEventPublisher(context);
((ConfigurableApplicationContext) context).addApplicationListener(relMongoPersistentEntityIndexCreator);
}
} else if (mappingContext instanceof MongoMappingContext && ((MongoMappingContext) mappingContext).isAutoIndexCreation()) {
logger.warn(
"Index auto creation is enabled on defautl MongoMappingContext, this will cause index creation problems on associations {} Consider using io.github.kaiso.relmongo.config.RelMongoMappingContext instead of spring default MongoMappingContext or disable index auto creation",
LINE_SEPARATOR);
}
}
});
}

}
Loading

0 comments on commit 8807992

Please sign in to comment.