18
18
import static org .hamcrest .CoreMatchers .*;
19
19
import static org .junit .Assert .*;
20
20
21
+ import lombok .Data ;
22
+
21
23
import java .util .Arrays ;
22
24
25
+ import org .bson .types .ObjectId ;
23
26
import org .junit .Before ;
24
27
import org .junit .Test ;
25
28
import org .junit .runner .RunWith ;
26
29
import org .springframework .beans .factory .annotation .Autowired ;
30
+ import org .springframework .data .annotation .Id ;
27
31
import org .springframework .data .mongodb .core .MongoOperations ;
32
+ import org .springframework .data .mongodb .core .mapping .Document ;
28
33
import org .springframework .data .mongodb .core .query .Query ;
29
34
import org .springframework .data .mongodb .repository .Person ;
30
35
import org .springframework .data .mongodb .repository .QPerson ;
@@ -49,7 +54,9 @@ public class QuerydslRepositorySupportTests {
49
54
@ Before
50
55
public void setUp () {
51
56
57
+ operations .remove (new Query (), Outer .class );
52
58
operations .remove (new Query (), Person .class );
59
+
53
60
person = new Person ("Dave" , "Matthews" );
54
61
operations .save (person );
55
62
@@ -97,4 +104,54 @@ public void shouldAllowDbRefAgainstIdProperty() {
97
104
assertThat (queryUsingIdField .fetchOne (), equalTo (person ));
98
105
assertThat (queryUsingIdField .fetchOne (), equalTo (queryUsingRefObject .fetchOne ()));
99
106
}
107
+
108
+ @ Test // DATAMONGO-1998
109
+ public void shouldLeaveStringIdThatIsNoValidObjectIdAsItIs () {
110
+
111
+ Outer outer = new Outer ();
112
+ outer .id = "outer-1" ;
113
+ outer .inner = new Inner ();
114
+ outer .inner .id = "inner-1" ;
115
+ outer .inner .value = "go climb a rock" ;
116
+
117
+ operations .save (outer );
118
+
119
+ QQuerydslRepositorySupportTests_Outer o = QQuerydslRepositorySupportTests_Outer .outer ;
120
+ SpringDataMongodbQuery <Outer > query = repoSupport .from (o ).where (o .inner .id .eq (outer .inner .id ));
121
+
122
+ assertThat (query .fetchOne (), equalTo (outer ));
123
+ }
124
+
125
+ @ Test // DATAMONGO-1998
126
+ public void shouldConvertStringIdThatIsAValidObjectIdIntoTheSuch () {
127
+
128
+ Outer outer = new Outer ();
129
+ outer .id = new ObjectId ().toHexString ();
130
+ outer .inner = new Inner ();
131
+ outer .inner .id = new ObjectId ().toHexString ();
132
+ outer .inner .value = "eat sleep workout repeat" ;
133
+
134
+ operations .save (outer );
135
+
136
+ QQuerydslRepositorySupportTests_Outer o = QQuerydslRepositorySupportTests_Outer .outer ;
137
+ SpringDataMongodbQuery <Outer > query = repoSupport .from (o ).where (o .inner .id .eq (outer .inner .id ));
138
+
139
+ assertThat (query .fetchOne (), equalTo (outer ));
140
+ }
141
+
142
+ @ Data
143
+ @ Document
144
+ public static class Outer {
145
+
146
+ @ Id String id ;
147
+ Inner inner ;
148
+ }
149
+
150
+ @ Data
151
+ public static class Inner {
152
+
153
+ @ Id String id ;
154
+ String value ;
155
+
156
+ }
100
157
}
0 commit comments