-
Aug 15th, 2011, 05:19 PM
#1
How to query documents with @DBRef using repositories
I have two collections: Address and Person. Person refers Address class.
@Document
public class Address {
@Id
private ObjectId id;
}
@Document
public class Person {
@Id
private ObjectId id;
@DBRef
private Address add;
//getters and setters
}
Previously, I used MongoTemplate for querying and I was able to query using $id reference. Now, after moving to M3 I am trying to switch my data layer to use spring data's repositories. Can you please advice on how the @DbRef will work with repos? For example, I want to do the following:
public interface AddressRepository extends MongoRepository<Person, ObjectId> {
}
public interface PersonRepository extends MongoRepository<Address, ObjectId> {
List<Person> findByAddress(Address a);
}
Thanks in advance.
Last edited by aachpach; Aug 15th, 2011 at 06:09 PM.
-
Sep 20th, 2011, 08:36 AM
#2
Hi,
I am stuck at the same. Want to update the collection. Do not know how to access using repository. Did you find the solution?
Thanks
Note: Since it is a recent thread, I am replying to this
-
Oct 9th, 2011, 08:10 AM
#3
Problem with @DbRef
I am stuck at the same place .. did anyone got any solution ?
-
Dec 26th, 2011, 10:04 PM
#4
-
Dec 28th, 2011, 04:55 AM
#5
Workaround
Hi,
I havn't solved it but found a work around that works.
Use following query?
class User {
@id
String objectId;
}
class Group {
@DbRef
List<User> participants;
}
mongoOperations.find(new Query(where("participants.$ref").is("User").and("p articipants.$id").is(user.objectId)), Group.class, "Group");
Apologies for not being accurate, don't have machine to run the example.
Hope it helps!!!
-
Jan 12th, 2012, 04:34 AM
#6
This is currently not supported. However there's a ticket [0] open to track this (which originated from this thread).
[0] https://jira.springsource.org/browse/DATAMONGO-347
-
Apr 17th, 2012, 09:00 AM
#7
Problem with @DbRef
I tried using the snapshot and am receiving the error:Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'mongoTemplate'
Could not instantiate bean class [org.springframework.data.mongodb.core.MongoTemplat e]: Constructor threw exception; nested exception is java.lang.NoSuchFieldError: NO_CREDENTIALS
What are the other dependencies I am missing?
-
Apr 18th, 2012, 01:30 AM
#8
Make sure you're also using the build snapshots of Spring Data Commons core.
-
Apr 18th, 2012, 02:13 AM
#9
yes, i forgot that i had to forcibly use commons core 1.2.1 before. When i commented out of my pom got it to work.Thanks!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules