I want to add an Attachment entity which I will be refering to from multiple different Entities, but it does not refer back to these, how do I get this working in ORMLite?
I keep getting this Exception:
Caused by: java.sql.SQLException: Foreign collection class entity.Attachment for
field 'attachments' column-name does not contain a foreign field named
'attachmentId' of class enity.News
For example I have a News Entity
@DatabaseTable
public class News extends Record {
@DatabaseField(index = true, id = true)
private long newsArticleId;
@DatabaseField
private String subject;
@DatabaseField
private String content;
@ForeignCollectionField
Collection<Attachment> attachments;
}
The Attachment Entity:
@DatabaseTable
public class Attachment extends Record {
@DatabaseField(id = true, index = true)
private long attachmentId;
@DatabaseField
private String attachmentUrl;
}
Could someone please point to me and laugh and tell me why I am doing this wrong and what I'm misunderstanding here. Thanks.
This is a FAQ. To quote from the ORMLite docs on foreign-collections:
In your example, for ORMLite to figure out which
Attachments a particularNewsentity has, theAttachmententity must have aNewsfield. The other way to do would be to have a join table, but ORMLite won't do that for you.