google-code-export / morphia

Automatically exported from code.google.com/p/morphia
1 stars 0 forks source link

About delete data #401

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Person:
@Entity
public class Person {
    @Id
    private Integer id;
    private String name;
    private Integer age;
    @Embedded
    List<Email> emails;
}
Email:
@Embedded
public class Email{
    @Id
    private Integer id;
    private String email;  
}
Data:
Person [id=1, name=shawn, age=20, 
       emails=[Email [id=1, email=abc@abc.com], 
               Email [id=2, email=aaa@aaa.com],
               Email [id=3, email=ccc@ccc.com]]
       ]
issue:
just want to delete email who's id=1 in this person who's id=1
the result i want is :
 Person [id=1, name=shawn, age=20, 
       emails=[Email [id=2, email=aaa@aaa.com],
               Email [id=3, email=ccc@ccc.com]]
       ]
what can i do with Query and UpdateOperations
thanks !

Original issue reported on code.google.com by GGshawn...@gmail.com on 28 Mar 2012 at 4:43

GoogleCodeExporter commented 9 years ago
Query<Person> query = ds.getQuery(Person.class).field("id").equal(person.getId)
                        .filter("emails.id", email.getId());
UpdateOperations<Person> ops 
=ds.getUpdateOperations(Person.class).disableValidation();
ops.removeAll("emails", new BasicDBObject("id", email.getId()));
ds.update(query, ops);

this is the solution

Original comment by GGshawn...@gmail.com on 29 Mar 2012 at 5:11

GoogleCodeExporter commented 9 years ago

Original comment by scotthernandez on 3 Apr 2012 at 6:28