0
votes

I'm building an application with Google App Engine. I'm using Objectify for Datastore persistence. I have two POJO class.

@Entity
public class Book
{
    @Id private Long id;
    private String name;
    private String isbn;
    private String author;
}

and

@Entity
public class Person {
    @Id private String email;
    private String password;
    private String name;
    private List<Book> myBooks;
}

A person can contain many books but book only belongs to one person. Currently I'm saving data like this.

//data from Front-end
Person p = new Person(email, password, name);
PersonDAO dao = new PersonDAO();
dao.save(p);

//...
//data from Front-end
Book b = new Book(name, author, isbn);
BookDAO daoBook = new BookDAO();
daoBook.save(b);

//...
Person q = dao.load(email);
q.addBook(b);
dao.save(q);

ObjectifyService ofy() with methods are implemented in DAO classes.

It's ok my implementation? How can I optimize the relationship? Every time that I need create a book this is saved like Entity but I need the relationship with a person, therefore I'm saving book twice. I've seen implementantions with Key, Ref @Load tags but I don't know how are working them.

Besides, Person POJO has a Date field. will It be saved normally?

1

1 Answers

0
votes

It has being quite a while since I wrote this code, but here it goes:

@Entity
public class Book {

    @Id
    private Long id;

    @Parent
    @ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
    private Key<User> user;

    private String author;

    private String isbn;

    private String name;

}

Take a look at Udacity Conference Central for full example. It is the final project of Developing Scalable Apps in Java with Google App Engine, by Udacity.