Announcement Announcement Module
Collapse
No announcement yet.
spring-data-mongo queries on subclass Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring-data-mongo queries on subclass

    Hi Ladies and Gents,
    I am new to mongo and spring-data-mongo. so please bare with me

    I have a test project with the following two classes. Person and Address.
    I am trying to write a Query criteria that will return a collection of 'Person' instances for a 'Person' who's address.city="x"

    The sample code:
    package org.spring.mongodb.example;


    import org.springframework.data.mongodb.core.MongoOperati ons;
    import org.springframework.data.mongodb.core.MongoTemplat e;
    import org.springframework.data.mongodb.core.query.Query;
    import org.springframework.data.mongodb.core.query.Criter ia;
    import com.mongodb.Mongo;


    public class MongoApp {


    public static void main(String[] args) throws Exception {

    MongoOperations mongoOps = new MongoTemplate(new Mongo(), "database");

    Address addr = new Address();
    addr.setCountry("contry");
    addr.setCity("city2");
    addr.setStreet("street");

    mongoOps.insert(new Person("Joe", 34, addr));

    Query personQuery = new Query(Criteria.where("name").is("Joe"));
    Person onePerson = mongoOps.findOne(personQuery, Person.class);
    System.out.println(onePerson);

    Query cityQuery = new Query(Criteria.where("address.city").is("city2"));
    Address oneAddr = mongoOps.findOne(cityQuery, Address.class);
    System.out.println(oneAddr);

    mongoOps.dropCollection("person");
    }
    }

    Below are the Person and Address classes:


    package org.spring.mongodb.example;

    public class Address {

    /* (non-Javadoc)
    * @see java.lang.Object#toString()
    */
    @Override
    public String toString() {
    return "Address [country=" + country + ", city=" + city + ", street="
    + street + "]";
    }
    private String country;
    private String city;
    private String street;

    public void setCountry(String country) {
    this.country = country;
    }
    public String getCountry() {
    return country;
    }
    public void setCity(String city) {
    this.city = city;
    }
    public String getCity() {
    return city;
    }
    public void setStreet(String street) {
    this.street = street;
    }
    public String getStreet() {
    return street;
    }



    }



    /**
    *
    */
    package org.spring.mongodb.example;

    /**
    * @author lahava
    *
    */
    public class Person {

    /* (non-Javadoc)
    * @see java.lang.Object#toString()
    */
    @Override
    public String toString() {
    return "Person [id=" + id + ", name=" + name + ", age=" + age
    + ", address=" + address + "]";
    }

    private String id;
    private String name;
    private int age;
    private Address address;

    public Person(String name, int age, Address addr) {
    this.name = name;
    this.age = age;
    this.address = addr;
    }

    public String getId() {
    return id;
    }

    public String getName() {
    return name;
    }

    public int getAge() {
    return age;
    }

    public void setAddress(Address address) {
    this.address = address;
    }

    public Address getAddress() {
    return address;
    }
    }


    Your help is most appreciated.
    Asaf
Working...
X