ORMLite(Relationship)
I explained how to use ORMLite in Android.
In this blog, I explained about easy relationship.
Database
1. Parent
id Integer
name String
2. Child
id Integer
name String
parent_id Integer
That means parent(one) versus child(many) one-many relationship.
How to make Entity
Let’s make entity class for them.
Parent
@DatabaseTable(tableName="parent")
public class Parent
{
@DatabaseField(generatedId=true)
private Integer id;
@DatabaseField
private String name;
@ForeignCollectionField
private ForeignCollection<Child> children;
// getter, setter
}
The key point is ForeignCollectionField, this is foreign key setting.
@DatabaseTable(tableName="child")
public class Child
{
@DatabaseField(generatedId=true)
private Integer id;
@DatabaseField
private String en;
@DatabaseField(foreign = true, foreignAutoRefresh = true)
private Parent parent;
// getter, setter
}
Parent in Child class is defined as parent_id in Child Table. That is point.
To access using DAO, you should use pareint_id
DAO
public List<Child> findByParent( ConnectionSource cs, Parent parent) throws SQLException
{
Dao<Child,Integer> dao = DaoManager.createDao(cs, Child.class);
return dao.queryForEq("parent_id", parent.getId());
}

helpful indeed 🙂 😀