Skip to content

Commit 8807992

Browse files
authored
Merge pull request #50 from kaiso/B#49
B#49
2 parents 97c3da7 + 61beb19 commit 8807992

22 files changed

+1080
-103
lines changed

pom.xml

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>io.github.kaiso.relmongo</groupId>
77
<artifactId>relmongo</artifactId>
8-
<version>3.2.0</version>
8+
<version>3.3.3</version>
99
<packaging>jar</packaging>
1010

1111
<name>relmongo</name>
@@ -18,8 +18,8 @@
1818
<maven.compiler.target>1.8</maven.compiler.target>
1919
<maven.plugin.source.version>3.0.1</maven.plugin.source.version>
2020
<maven.plugin.javadoc.version>3.0.1</maven.plugin.javadoc.version>
21-
<spring.data.version>2.2.1.RELEASE</spring.data.version>
22-
<spring.version>5.2.1.RELEASE</spring.version>
21+
<spring.data.version>2.2.6.RELEASE</spring.data.version>
22+
<spring.version>5.2.5.RELEASE</spring.version>
2323
<mongo.driver.version>3.11.2</mongo.driver.version>
2424
<junit.jupiter.version>5.4.1</junit.jupiter.version>
2525
<junit.platform.version>1.4.2</junit.platform.version>
@@ -64,14 +64,14 @@
6464
<version>${spring.data.version}</version>
6565
<scope>provided</scope>
6666
</dependency>
67-
67+
6868
<dependency>
6969
<groupId>org.springframework</groupId>
7070
<artifactId>spring-beans</artifactId>
7171
<version>${spring.version}</version>
7272
<scope>provided</scope>
7373
</dependency>
74-
74+
7575
<dependency>
7676
<groupId>org.springframework</groupId>
7777
<artifactId>spring-core</artifactId>
@@ -81,9 +81,9 @@
8181

8282

8383
<dependency>
84-
<groupId>org.mongodb</groupId>
85-
<artifactId>mongodb-driver-async</artifactId>
86-
<version>${mongo.driver.version}</version>
84+
<groupId>org.mongodb</groupId>
85+
<artifactId>mongodb-driver-async</artifactId>
86+
<version>${mongo.driver.version}</version>
8787
</dependency>
8888

8989
<!-- test dependencies -->
@@ -146,6 +146,18 @@
146146
<version>2.11.1</version>
147147
<scope>test</scope>
148148
</dependency>
149+
<dependency>
150+
<groupId>com.fasterxml.jackson.dataformat</groupId>
151+
<artifactId>jackson-dataformat-yaml</artifactId>
152+
<version>2.7.3</version>
153+
<scope>test</scope>
154+
</dependency>
155+
<dependency>
156+
<groupId>com.fasterxml.jackson.core</groupId>
157+
<artifactId>jackson-databind</artifactId>
158+
<version>2.5.4</version>
159+
<scope>test</scope>
160+
</dependency>
149161
</dependencies>
150162

151163

src/main/java/io/github/kaiso/relmongo/annotation/JoinProperty.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
@Target(ElementType.FIELD)
3131
public @interface JoinProperty {
3232
/**
33-
* the name of the attribute that holds the reference to the associated object
33+
* the name of the attribute that holds the reference to the associated object
34+
* @return name
3435
*/
3536
String name() default "";
3637
}

src/main/java/io/github/kaiso/relmongo/annotation/ManyToOne.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package io.github.kaiso.relmongo.annotation;
1818

19-
import org.springframework.data.annotation.Reference;
20-
2119
import java.lang.annotation.ElementType;
2220
import java.lang.annotation.Retention;
2321
import java.lang.annotation.RetentionPolicy;
@@ -32,7 +30,7 @@
3230
*/
3331
@Retention(RetentionPolicy.RUNTIME)
3432
@Target(ElementType.FIELD)
35-
@Reference
33+
@RelMongoAnnotation
3634
public @interface ManyToOne {
3735
/**
3836
* (Optional) Whether the association should be lazily loaded or must be eagerly

src/main/java/io/github/kaiso/relmongo/annotation/OneToMany.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package io.github.kaiso.relmongo.annotation;
1818

19-
import org.springframework.data.annotation.Reference;
20-
2119
import java.lang.annotation.ElementType;
2220
import java.lang.annotation.Retention;
2321
import java.lang.annotation.RetentionPolicy;
@@ -32,9 +30,9 @@
3230
*/
3331
@Retention(RetentionPolicy.RUNTIME)
3432
@Target(ElementType.FIELD)
35-
@Reference
33+
@RelMongoAnnotation
3634
public @interface OneToMany {
37-
35+
3836
/**
3937
* (Optional) Whether the association should be lazily loaded or must be eagerly
4038
* fetched.
@@ -51,6 +49,7 @@
5149
* (Optional) Whether to apply the remove operation to documents that have been
5250
* removed from the
5351
* relationship and to cascade the remove operation to those documents.
52+
*
5453
* @since 2.3.0
5554
*/
5655
boolean orphanRemoval() default false;

src/main/java/io/github/kaiso/relmongo/annotation/OneToOne.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package io.github.kaiso.relmongo.annotation;
1818

19-
import org.springframework.data.annotation.Reference;
20-
2119
import java.lang.annotation.ElementType;
2220
import java.lang.annotation.Retention;
2321
import java.lang.annotation.RetentionPolicy;
@@ -32,7 +30,7 @@
3230
*/
3331
@Retention(RetentionPolicy.RUNTIME)
3432
@Target(ElementType.FIELD)
35-
@Reference
33+
@RelMongoAnnotation
3634
public @interface OneToOne {
3735
/**
3836
* (Optional) Whether the association should be lazily loaded or must be eagerly
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.github.kaiso.relmongo.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(value = { ElementType.ANNOTATION_TYPE })
10+
public @interface RelMongoAnnotation {
11+
12+
}

src/main/java/io/github/kaiso/relmongo/config/EnableRelMongo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
*/
3636
@Retention(RetentionPolicy.RUNTIME)
3737
@Target(value = { ElementType.TYPE })
38-
@Import(value = PersistenceConfiguration.class)
38+
@Import(value = RelMongoConfiguration.class)
3939
public @interface EnableRelMongo {
4040

4141
}

src/main/java/io/github/kaiso/relmongo/config/EnableRelationalMongo.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@
2222
import java.lang.annotation.Retention;
2323
import java.lang.annotation.RetentionPolicy;
2424
import java.lang.annotation.Target;
25+
2526
/**
26-
* @deprecated use {@link EnableRelMongo} instead
27+
* @deprecated use {@link EnableRelMongo} instead
2728
* @author Kais OMRI
2829
*
2930
*/
3031
@Deprecated
3132
@Retention(RetentionPolicy.RUNTIME)
3233
@Target(value = { ElementType.TYPE })
33-
@Import(value = PersistenceConfiguration.class)
34+
@Import(value = RelMongoConfiguration.class)
3435
public @interface EnableRelationalMongo {
3536

3637
}

src/main/java/io/github/kaiso/relmongo/config/PersistenceConfiguration.java

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
2+
/**
3+
* Copyright 2018 Kais OMRI and authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package io.github.kaiso.relmongo.config;
19+
20+
import io.github.kaiso.relmongo.events.processor.RelMongoProcessor;
21+
22+
import org.slf4j.Logger;
23+
import org.slf4j.LoggerFactory;
24+
import org.springframework.context.ApplicationContext;
25+
import org.springframework.context.ApplicationEventPublisherAware;
26+
import org.springframework.context.ConfigurableApplicationContext;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.context.support.GenericApplicationContext;
30+
import org.springframework.data.mapping.context.MappingContext;
31+
import org.springframework.data.mongodb.core.MongoOperations;
32+
import org.springframework.data.mongodb.core.MongoTemplate;
33+
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
34+
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
35+
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
36+
37+
import java.util.concurrent.CompletableFuture;
38+
39+
/**
40+
*
41+
* @author Kais OMRI (kaiso)
42+
*
43+
*/
44+
@Configuration
45+
public class RelMongoConfiguration {
46+
47+
private static final String LINE_SEPARATOR = System.lineSeparator();
48+
49+
private static final Logger logger = LoggerFactory.getLogger(RelMongoConfiguration.class);
50+
51+
private final ApplicationContext context;
52+
53+
public RelMongoConfiguration(ApplicationContext context) {
54+
super();
55+
this.context = context;
56+
this.setUp();
57+
}
58+
59+
@Bean
60+
public RelMongoProcessor mongoEventListener(MongoOperations mongoOperations) {
61+
return new RelMongoProcessor(mongoOperations);
62+
}
63+
64+
65+
public void setUp() {
66+
CompletableFuture.runAsync(() -> {
67+
if (context.getBeanNamesForType(MongoTemplate.class).length > 0) {
68+
MongoTemplate template = context.getBean(MongoTemplate.class);
69+
MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext = template.getConverter().getMappingContext();
70+
if (mappingContext instanceof RelMongoMappingContext) {
71+
RelMongoMappingContext relMongoMappingContext = (RelMongoMappingContext) mappingContext;
72+
if (relMongoMappingContext.isAutoIndexCreation()) {
73+
logger.warn(
74+
"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",
75+
LINE_SEPARATOR);
76+
77+
RelMongoPersistentEntityIndexResolver indexResolver = new RelMongoPersistentEntityIndexResolver(relMongoMappingContext);
78+
RelMongoPersistentEntityIndexCreator relMongoPersistentEntityIndexCreator = new RelMongoPersistentEntityIndexCreator(
79+
relMongoMappingContext,
80+
template,
81+
indexResolver);
82+
83+
((GenericApplicationContext) context).registerBean("relMongoPersistentEntityIndexCreator",
84+
RelMongoPersistentEntityIndexCreator.class,
85+
() -> relMongoPersistentEntityIndexCreator,
86+
bd -> bd.setLazyInit(false));
87+
88+
((ApplicationEventPublisherAware) mappingContext).setApplicationEventPublisher(context);
89+
((ConfigurableApplicationContext) context).addApplicationListener(relMongoPersistentEntityIndexCreator);
90+
}
91+
} else if (mappingContext instanceof MongoMappingContext && ((MongoMappingContext) mappingContext).isAutoIndexCreation()) {
92+
logger.warn(
93+
"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",
94+
LINE_SEPARATOR);
95+
}
96+
}
97+
});
98+
}
99+
100+
}

0 commit comments

Comments
 (0)