Skip to content

Instantly share code, notes, and snippets.

@mcatta
Last active June 18, 2017 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcatta/ce71597b59b7fc8975d73c0d8186b99f to your computer and use it in GitHub Desktop.
Save mcatta/ce71597b59b7fc8975d73c0d8186b99f to your computer and use it in GitHub Desktop.
@Table(database=MyLibrary.class)
public class Book extends BaseModel {
@PrimaryKey(autoincrement = true)
int id;
@Column
String title;
@Column
int pages;
// Authors
List<Author> authors;
@OneToMany(methods = OneToMany.Method.ALL, variableName = "authors")
public List<Author> oneToManyAuthors() {
if (authors == null) {
authors = SQLite.select()
.from(Author.class)
.where(Author_Table.book_id.eq(id))
.queryList();
}
return authors;
}
Override
public boolean save() {
boolean res = super.save();
if (authors != null) {
for (Author s : authors) {
s.setBook(this);
s.save();
}
}
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment