Suppose that I am developing an android application for the storage of books. I have a table "Book". In which there is a link to the table "Author".
Book.java
@DatabaseTable(tableName = "book")
public class Book {
@DatabaseField(id = true)
private int id;
@DatabaseField(foreign = true, foreignAutoCreate = true, foreignAutoRefresh = true)
public Author author;
public Book() {
}
}
Author.java
@DatabaseTable(tableName = "author")
public class Author implements Serializable{
private static final long serialVersionUID = 6756426974931436504L;
@DatabaseField(generatedId = true)
private int id;
@DatabaseField
public String name;
public Author() {
}
}
When I get a response from the server, Robospice transmits the book data CacheManager to store in DB using OrmLite.
When OrmLite create row (Book) in DB, it checks the availability of nested objects (in this case the author) and before creating Book row OrmLite create Author row and put author_id in Book.
When re-saving (the book row already exists) Book object in the database OrmLite calls the update method, in which there is no saving of nested objects (Author), and record book will be given author_id = null.
Why the update method in OrmLite does't create nested object, maybe can you recommend better solutions?
Aucun commentaire:
Enregistrer un commentaire