Skip to content

DATAMONGO-1394 - Handle id path for references correctly when using Querydsl. #373

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
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>1.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1394-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.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1394-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.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1394-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.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1394-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.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1394-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.10.0.BUILD-SNAPSHOT</version>
<version>1.10.0.DATAMONGO-1394-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-2015 the original author or authors.
* Copyright 2011-2016 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 Down Expand Up @@ -29,6 +29,9 @@

import com.mongodb.DBObject;
import com.mongodb.DBRef;
import com.querydsl.core.types.Constant;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.Operation;
import com.querydsl.core.types.Path;
import com.querydsl.core.types.PathMetadata;
import com.querydsl.core.types.PathType;
Expand Down Expand Up @@ -100,7 +103,6 @@ protected DBObject asDBObject(String key, Object value) {
if (ID_KEY.equals(key)) {
return mapper.getMappedObject(super.asDBObject(key, value), null);
}

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

Expand All @@ -111,7 +113,7 @@ protected DBObject asDBObject(String key, Object value) {
@Override
protected boolean isReference(Path<?> path) {

MongoPersistentProperty property = getPropertyFor(path);
MongoPersistentProperty property = getPropertyForPotentialDbRef(path);
return property == null ? false : property.isAssociation();
}

Expand All @@ -121,7 +123,50 @@ protected boolean isReference(Path<?> path) {
*/
@Override
protected DBRef asReference(Object constant) {
return converter.toDBRef(constant, null);
return asReference(constant, null);
}

protected DBRef asReference(Object constant, Path<?> path) {
return converter.toDBRef(constant, getPropertyForPotentialDbRef(path));
}

@Override
protected String asDBKey(Operation<?> expr, int index) {

Expression<?> arg = expr.getArg(index);
if (arg instanceof Path) {

Path<?> path = (Path<?>) arg;
if (isReference(path)) {

MongoPersistentProperty property = getPropertyFor(path);
if (property.isIdProperty()) {
return super.asDBKey(expr, index).replaceAll("." + ID_KEY + "$", "");
}
}

}
return super.asDBKey(expr, index);
}

/*
* (non-Javadoc)
* @see com.querydsl.mongodb.MongodbSerializer#convert(com.querydsl.core.types.Path, com.querydsl.core.types.Constant)
*/
protected Object convert(Path<?> path, Constant<?> constant) {

if (isReference(path)) {

MongoPersistentProperty property = getPropertyFor(path);

if (property.isIdProperty()) {
return asReference(constant.getConstant(), path.getMetadata().getParent());
}

return asReference(constant.getConstant(), path);
}

return super.convert(path, constant);
}

private MongoPersistentProperty getPropertyFor(Path<?> path) {
Expand All @@ -135,4 +180,26 @@ private MongoPersistentProperty getPropertyFor(Path<?> path) {
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(parent.getType());
return entity != null ? entity.getPersistentProperty(path.getMetadata().getName()) : null;
}

/**
* Checks the given {@literal path} for referencing the {@literal id} property of a {@link DBRef} referenced object.
* If so it returns the referenced {@link MongoPersistentProperty} of the {@link DBRef} instead of the {@literal id}
* property.
*
* @param path
* @return
*/
private MongoPersistentProperty getPropertyForPotentialDbRef(Path<?> path) {

if (path == null) {
return null;
}

MongoPersistentProperty property = getPropertyFor(path);
if (property != null && property.isIdProperty() && path.getMetadata() != null
&& path.getMetadata().getParent() != null) {
return getPropertyFor(path.getMetadata().getParent());
}
return property;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011-2014 the original author or authors.
* Copyright 2011-2016 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 Down Expand Up @@ -28,6 +28,7 @@
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.repository.Person;
import org.springframework.data.mongodb.repository.QPerson;
import org.springframework.data.mongodb.repository.User;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

Expand All @@ -43,13 +44,16 @@ public class QuerydslRepositorySupportTests {

@Autowired MongoOperations operations;
Person person;
QuerydslRepositorySupport repoSupport;

@Before
public void setUp() {

operations.remove(new Query(), Person.class);
person = new Person("Dave", "Matthews");
operations.save(person);

repoSupport = new QuerydslRepositorySupport(operations) {};
}

@Test
Expand All @@ -72,10 +76,32 @@ public void shouldAllowAny() {
operations.save(person);

QPerson p = QPerson.person;
QuerydslRepositorySupport support = new QuerydslRepositorySupport(operations) {};

SpringDataMongodbQuery<Person> query = support.from(p).where(p.skills.any().in("guitarist"));
SpringDataMongodbQuery<Person> query = repoSupport.from(p).where(p.skills.any().in("guitarist"));

assertThat(query.fetchOne(), is(person));
}

/**
* @see DATAMONGO-1394
*/
@Test
public void shouldAllowDbRefAgainstIdProperty() {

User bart = new User();
bart.setUsername("bart@simpson.com");
operations.save(bart);

person.setCoworker(bart);
operations.save(person);

QPerson p = QPerson.person;

SpringDataMongodbQuery<Person> queryUsingIdField = repoSupport.from(p).where(p.coworker.id.eq(bart.getId()));
SpringDataMongodbQuery<Person> queryUsingRefObject = repoSupport.from(p).where(p.coworker.eq(bart));

assertThat(queryUsingIdField.fetchOne(), equalTo(person));
assertThat(queryUsingIdField.fetchOne(), equalTo(queryUsingRefObject.fetchOne()));
}

}