I'm using OrmLite on android for a structure simmilar to the following :
The Book class has a collection of primary BookArticle and a collection of secondary BookArticle :
@DatabaseTable(tableName = "BookV1", daoClass = BookDaoImplV1.class)
public class Book implements IBook {
@DatabaseField(id = true)
private String id;
@ForeignCollectionField(eager = false)
private ForeignCollection<BookArticle> primaryArticles;
@ForeignCollectionField(eager = false)
private ForeignCollection<BookArticle> secondaryArticles;
// constructor getters setters etc...
}
Then the BookArticle is declared as follow :
@DatabaseTable(tableName = "BookArticleV1", daoClass = BookArticleDaoImplV1.class)
public class BookArticle implements IBookArticle {
@DatabaseField(id = true)
private String id;
@DatabaseField
private String title;
@DatabaseField
private String summary;
// for ORM mapping only
@DatabaseField(foreign = true, foreignAutoRefresh = true, columnName = BOOK_FIELDNAME, index = true)
private Book book;
// constructor getters setters etc..
}
now saving the BookArticles and Book with DAO works fine however when I want to fetch my Book entity from database and access either primary or secondary articles (after a refresh() ) I have a problem because both collections hold ALL the articles (both primary and secondary) that have the book's id in their 'book' column.
Obviously I would need these primary and secondary articles to be separated when fetched from DB.
I would have expected "foreignFieldName" to be the answer to my problem.
@ForeignCollectionField(eager = false, foreignFieldName = "secondaryArticles")
private ForeignCollection<BookArticle> secondaryArticles;
but apparently it does not work that way.
Is there a way for me to differenciate between these two collections ? Maybe using a simple annotation argument such as "owningFieldName" or similar that would get persisted in DB alongside the BookArticle data?
Many thanks for your help.
Alex
I ran into the same problem.
Not a real solution but you can try to use only one list with a boolean for switching between primary/secondary articles like this:
@DatabaseTable(tableName = "BookV1", daoClass = BookDaoImplV1.class)
public class Book implements IBook {
@DatabaseField(id = true)
private String id;
@ForeignCollectionField(eager = false)
private ForeignCollection<BookArticle> articles;
// constructor getters setters etc...
}
And then for the BookArticle class
@DatabaseTable(tableName = "BookArticleV1", daoClass = BookArticleDaoImplV1.class)
public class BookArticle implements IBookArticle {
@DatabaseField(id = true)
private String id;
@DatabaseField
private String title;
@DatabaseField
private String summary;
@DatabaseField
private boolean isSecondary;
// for ORM mapping only
@DatabaseField(foreign = true, foreignAutoRefresh = true, columnName = BOOK_FIELDNAME, index = true)
private Book book;
// constructor getters setters etc..
}
Hoping to see a real answer
if you want to add multible ForeignerCollections form the same typ to one class, you have to add 2 fields for ORM-Mapping in the BookArticle class and link this in the Book class to the ORM-Mapping fields.
e.g
Book class
@ForeignCollectionField(eager = false, foreignFieldName = "primaryBook")
private ForeignCollection<BookArticle> primaryArticles;
@ForeignCollectionField(eager = false, foreignFieldName = "secondaryBook")
private ForeignCollection<BookArticle> secondaryArticles;
BookArticle class
// for ORM mapping primary Books
@DatabaseField(foreign = true, foreignAutoRefresh = true, columnName = BOOK_FIELDNAME, index = true)
private Book primaryBook;
// for ORM mapping secondary Books
@DatabaseField(foreign = true, foreignAutoRefresh = true, columnName = BOOK_FIELDNAME, index = true)
private Book secondaryBook;
When you call now bookDao.queryAll() and access the secondary Articles list, you will see the articles are correctly linked.
The disadvantage is, that you have 2 columes in the BookArticle Table for foreigner keys.
Greeting, Codenix
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With