Example usage for org.springframework.data.mongodb.core.mapping Person Person

List of usage examples for org.springframework.data.mongodb.core.mapping Person Person

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core.mapping Person Person.

Prototype

@PersistenceConstructor
    public Person(Integer ssn, String firstName, String lastName, Integer age, T address) 

Source Link

Usage

From source file:org.springframework.data.mongodb.core.mapping.MappingTests.java

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testWriteEntity() {

    Address addr = new Address();
    addr.setLines(new String[] { "1234 W. 1st Street", "Apt. 12" });
    addr.setCity("Anytown");
    addr.setPostalCode(12345);/*from w w w . j  a va  2  s.c  o  m*/
    addr.setCountry("USA");

    Account acct = new Account();
    acct.setBalance(1000.00f);
    template.insert(acct, "account");

    List<Account> accounts = new ArrayList<Account>();
    accounts.add(acct);

    Person p = new Person(123456789, "John", "Doe", 37, addr);
    p.setAccounts(accounts);
    template.insert(p, "person");

    Account newAcct = new Account();
    newAcct.setBalance(10000.00f);
    template.insert(newAcct, "account");

    accounts.add(newAcct);
    template.save(p, "person");

    assertNotNull(p.getId());

    List<Person> result = template.find(new Query(Criteria.where("ssn").is(123456789)), Person.class);
    assertThat(result.size(), is(1));
    assertThat(result.get(0).getAddress().getCountry(), is("USA"));
    assertThat(result.get(0).getAccounts(), notNullValue());
}

From source file:org.springframework.data.mongodb.core.mapping.MappingTests.java

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void testUniqueIndex() {
    Address addr = new Address();
    addr.setLines(new String[] { "1234 W. 1st Street", "Apt. 12" });
    addr.setCity("Anytown");
    addr.setPostalCode(12345);//from   w  w  w  .  ja v a 2s  . c  om
    addr.setCountry("USA");

    Person p1 = new Person(1234567890, "John", "Doe", 37, addr);
    Person p2 = new Person(1234567890, "Jane", "Doe", 38, addr);

    List<Person> persons = new ArrayList<Person>();
    persons.add(p1);
    persons.add(p2);
    template.insert(persons, MongoCollectionUtils.getPreferredCollectionName(Person.class));

    List<Person> result = template.find(new Query(Criteria.where("ssn").is(1234567890)), Person.class);
    assertThat(result.size(), is(1));
}

From source file:org.springframework.data.mongodb.core.mapping.MappingTests.java

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testQueryUpdate() {
    Address addr = new Address();
    addr.setLines(new String[] { "1234 W. 1st Street", "Apt. 12" });
    addr.setCity("Anytown");
    addr.setPostalCode(12345);/* w  ww.  ja v a  2 s.  c om*/
    addr.setCountry("USA");

    Person p = new Person(1111, "Query", "Update", 37, addr);
    template.insert(p);

    addr.setCity("New Town");
    template.updateFirst(query(where("ssn").is(1111)), update("address", addr), Person.class);

    Person p2 = template.findOne(query(where("ssn").is(1111)), Person.class);
    assertThat(p2.getAddress().getCity(), is("New Town"));
}