Skip to content

DATAMONGO-972 - Handle references correctly when using QueryDSL. #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.6.0.BUILD-SNAPSHOT</version>
<version>1.6.0.DATAMONGO-972-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-mongodb-cross-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.6.0.BUILD-SNAPSHOT</version>
<version>1.6.0.DATAMONGO-972-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.6.0.BUILD-SNAPSHOT</version>
<version>1.6.0.DATAMONGO-972-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.6.0.BUILD-SNAPSHOT</version>
<version>1.6.0.DATAMONGO-972-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-log4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.6.0.BUILD-SNAPSHOT</version>
<version>1.6.0.DATAMONGO-972-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.6.0.BUILD-SNAPSHOT</version>
<version>1.6.0.DATAMONGO-972-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2012 the original author or authors.
* Copyright 2011-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,8 @@

import java.util.regex.Pattern;

import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.annotation.Reference;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.convert.QueryMapper;
Expand All @@ -25,7 +27,10 @@
import org.springframework.util.Assert;

import com.mongodb.DBObject;
import com.mongodb.DBRef;
import com.mysema.query.mongodb.MongodbSerializer;
import com.mysema.query.types.Constant;
import com.mysema.query.types.Operation;
import com.mysema.query.types.Path;
import com.mysema.query.types.PathMetadata;
import com.mysema.query.types.PathType;
Expand All @@ -34,6 +39,7 @@
* Custom {@link MongodbSerializer} to take mapping information into account when building keys for constraints.
*
* @author Oliver Gierke
* @author Christoph Strobl
*/
class SpringDataMongodbSerializer extends MongodbSerializer {

Expand Down Expand Up @@ -86,4 +92,45 @@ protected DBObject asDBObject(String key, Object value) {

return super.asDBObject(key, value instanceof Pattern ? value : converter.convertToMongoType(value));
}

@Override
protected boolean isReference(Path<?> arg) {
return AnnotationUtils.getAnnotation(arg.getAnnotatedElement(), Reference.class) != null;
}

/*
* (non-Javadoc)
* @see com.mysema.query.mongodb.MongodbSerializer#asReference(java.lang.Object)
*/
@Override
protected DBRef asReference(Object constant) {
return converter.toDBRef(constant, null);
}

/*
* (non-Javadoc)
* @see com.mysema.query.mongodb.MongodbSerializer#asReference(com.mysema.query.types.Operation, int)
*/
@Override
protected DBRef asReference(Operation<?> expr, int constIndex) {

for (Object arg : expr.getArgs()) {
if (arg instanceof Path) {
return convertToDBRef(((Constant<?>) expr.getArg(constIndex)).getConstant(), (Path<?>) arg);
}
}

return super.asReference(expr, constIndex);
}

private DBRef convertToDBRef(Object source, Path<?> path) {

MongoPersistentProperty property = null;
if (path.getMetadata().getParent() != null) {
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(path.getMetadata().getParent().getType());
property = entity != null ? entity.getPersistentProperty(path.getMetadata().getName()) : null;
}

return converter.toDBRef(source, property);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ public Person withAddress(Address address) {
return this;
}

public void setCreator(User creator) {
this.creator = creator;
}

/*
* (non-Javadoc)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package org.springframework.data.mongodb.repository;

import static org.hamcrest.core.Is.*;
import static org.junit.Assert.*;

import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;

/**
Expand All @@ -24,4 +28,30 @@
* @author Thomas Darimont
*/
@ContextConfiguration
public class PersonRepositoryIntegrationTests extends AbstractPersonRepositoryIntegrationTests {}
public class PersonRepositoryIntegrationTests extends AbstractPersonRepositoryIntegrationTests {

/**
* <strong>ATTENTION</strong>: <br/>
* Test requires {@literal com.mysema.querydsl:querydsl-mongodb:3.4.1} to run!<br />
* Run with: {@code mvn -Dquerydsl=3.4.1 clean install}. <br />
* <br />
* TODO: move this one to AbstractPersonRepositoryIntegrationTests.
*
* @see DATAMONGO-972
*/
@Test
public void shouldExecuteFindOnDbRefCorrectly() {

operations.remove(new org.springframework.data.mongodb.core.query.Query(), User.class);

User user = new User();
user.setUsername("Valerie Matthews");

operations.save(user);

dave.setCreator(user);
operations.save(dave);

assertThat(repository.findOne(QPerson.person.creator.eq(user)), is(dave));
}
}