Announcement Announcement Module
Collapse
No announcement yet.
How to query documents with @DBRef using repositories Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 06:09 PM.

  • #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

    Comment


    • #3
      Problem with @DbRef

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

      Comment


      • #4
        Anyone solve this?

        Comment


        • #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!!!

          Comment


          • #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

            Comment


            • #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?

              Comment


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

                Comment


                • #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!

                  Comment

                  Working...
                  X