Results 1 to 9 of 9

Thread: How to query documents with @DBRef using repositories

  1. #1
    Join Date
    Jun 2011
    Posts
    3

    Default 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.

  2. #2

    Default

    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

  3. #3

    Default Problem with @DbRef

    I am stuck at the same place .. did anyone got any solution ?

  4. #4

    Default

    Anyone solve this?

  5. #5

    Default 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!!!

  6. #6
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    492

    Default

    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

  7. #7

    Default 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?

  8. #8
    Join Date
    Apr 2006
    Location
    Dresden, Germany
    Posts
    492

    Default

    Make sure you're also using the build snapshots of Spring Data Commons core.

  9. #9

    Default

    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
  •