Skip to content

Commit e2f3966

Browse files
christophstroblodrotbohm
authored andcommitted
DATAMONGO-972 - Querydsl integration now handles references correctly.
SpringDataMongodbSerializer now overrides the necessary methods to create the appropriate DBRef objects when serializing data via Querydsl. We currently disable the test case as it the fix taking effect requires Querydsl 3.4.1 which unfortunately breaks Java 6 compatibility. We include the fix nonetheless to allow users on Java 7 to potentially use the latest Querydsl. Original pull request: #203. Related tickets: querydsl/querydsl#803.
1 parent 2b3e546 commit e2f3966

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SpringDataMongodbSerializer.java

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2012 the original author or authors.
2+
* Copyright 2011-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,7 +25,10 @@
2525
import org.springframework.util.Assert;
2626

2727
import com.mongodb.DBObject;
28+
import com.mongodb.DBRef;
2829
import com.mysema.query.mongodb.MongodbSerializer;
30+
import com.mysema.query.types.Constant;
31+
import com.mysema.query.types.Operation;
2932
import com.mysema.query.types.Path;
3033
import com.mysema.query.types.PathMetadata;
3134
import com.mysema.query.types.PathType;
@@ -34,6 +37,7 @@
3437
* Custom {@link MongodbSerializer} to take mapping information into account when building keys for constraints.
3538
*
3639
* @author Oliver Gierke
40+
* @author Christoph Strobl
3741
*/
3842
class SpringDataMongodbSerializer extends MongodbSerializer {
3943

@@ -44,7 +48,7 @@ class SpringDataMongodbSerializer extends MongodbSerializer {
4448
/**
4549
* Creates a new {@link SpringDataMongodbSerializer} for the given {@link MappingContext}.
4650
*
47-
* @param mappingContext
51+
* @param mappingContext must not be {@literal null}.
4852
*/
4953
public SpringDataMongodbSerializer(MongoConverter converter) {
5054

@@ -86,4 +90,57 @@ protected DBObject asDBObject(String key, Object value) {
8690

8791
return super.asDBObject(key, value instanceof Pattern ? value : converter.convertToMongoType(value));
8892
}
93+
94+
/*
95+
* (non-Javadoc)
96+
* @see com.mysema.query.mongodb.MongodbSerializer#isReference(com.mysema.query.types.Path)
97+
*/
98+
@Override
99+
protected boolean isReference(Path<?> path) {
100+
101+
MongoPersistentProperty property = getPropertyFor(path);
102+
return property == null ? false : property.isAssociation();
103+
}
104+
105+
/*
106+
* (non-Javadoc)
107+
* @see com.mysema.query.mongodb.MongodbSerializer#asReference(java.lang.Object)
108+
*/
109+
@Override
110+
protected DBRef asReference(Object constant) {
111+
return converter.toDBRef(constant, null);
112+
}
113+
114+
/*
115+
* (non-Javadoc)
116+
* @see com.mysema.query.mongodb.MongodbSerializer#asReference(com.mysema.query.types.Operation, int)
117+
*/
118+
@Override
119+
protected DBRef asReference(Operation<?> expr, int constIndex) {
120+
121+
for (Object arg : expr.getArgs()) {
122+
123+
if (arg instanceof Path) {
124+
125+
MongoPersistentProperty property = getPropertyFor((Path<?>) arg);
126+
Object constant = ((Constant<?>) expr.getArg(constIndex)).getConstant();
127+
128+
return converter.toDBRef(constant, property);
129+
}
130+
}
131+
132+
return super.asReference(expr, constIndex);
133+
}
134+
135+
private MongoPersistentProperty getPropertyFor(Path<?> path) {
136+
137+
Path<?> parent = path.getMetadata().getParent();
138+
139+
if (parent == null) {
140+
return null;
141+
}
142+
143+
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(parent.getType());
144+
return entity != null ? entity.getPersistentProperty(path.getMetadata().getName()) : null;
145+
}
89146
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.List;
2525

2626
import org.junit.Before;
27+
import org.junit.Ignore;
2728
import org.junit.Test;
2829
import org.junit.runner.RunWith;
2930
import org.springframework.beans.factory.annotation.Autowired;
@@ -918,4 +919,26 @@ public void findByCustomQueryLastnameAndStreetInList() {
918919
assertThat(result.getTotalPages(), is(2));
919920
assertThat(result.getTotalElements(), is(3L));
920921
}
922+
923+
/**
924+
* Ignored for now as this requires Querydsl 3.4.1 to succeed.
925+
*
926+
* @see DATAMONGO-972
927+
*/
928+
@Test
929+
@Ignore
930+
public void shouldExecuteFindOnDbRefCorrectly() {
931+
932+
operations.remove(new org.springframework.data.mongodb.core.query.Query(), User.class);
933+
934+
User user = new User();
935+
user.setUsername("Valerie Matthews");
936+
937+
operations.save(user);
938+
939+
dave.setCreator(user);
940+
operations.save(dave);
941+
942+
assertThat(repository.findOne(QPerson.person.creator.eq(user)), is(dave));
943+
}
921944
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/Person.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,10 @@ public Person withAddress(Address address) {
267267
return this;
268268
}
269269

270+
public void setCreator(User creator) {
271+
this.creator = creator;
272+
}
273+
270274
/*
271275
* (non-Javadoc)
272276
*

0 commit comments

Comments
 (0)