HyperX
HyperX

Reputation: 1211

One to many in DB Flow

I am trying to use DBFlow with my objects where i have relationship One TO Many (User have many Businesses), i was following tutorial at their offical wiki but it just dont want to work.

Here are my user:

@Column
@PrimaryKey(autoincrement = true)
private Integer id;

@Expose
@Column
private Boolean allow_offline;

@Expose
@Column
private Integer user_level;

@Expose
@Column
private Integer account_validity;

@Expose
@Column
private Integer selected_business;

@Expose
@Column
private String user_id;

@Expose
@Column
private Boolean allow_print;

@Expose
@Column
private String email;

@Expose
private List<Business> businesses;

and my business

@Expose
@Column
@PrimaryKey
private Integer id;
@Expose
@Column
private String name;
@Expose
@Column
private Boolean is_zero_tax;
@Expose
@Column
private String header;
@Expose
@Column
private String footer;

Should i be making a method at Business like associateBusinessWithUser ? Or how should i be linking this?

Upvotes: 1

Views: 2189

Answers (1)

Defuera
Defuera

Reputation: 5506

here's the code for dbFlowVersion = '3.0.0-beta1'. I simplify your classes:

    @Table(database = DbFlowDataBase.class)
    public class User extends BaseModel {

    @PrimaryKey(autoincrement = true)
    Integer id;

    List<Business> businesses;

    @OneToMany(methods = OneToMany.Method.ALL, variableName = "businesses")
    public Field[] dbFlowOneTwoManyUtilMethod() {
        if (businesses == null) {
            businesses = SQLite.select()
                    .from(Business.class)
                    .where(Business_Table.userId.eq(id))
                    .queryList();
        }
        return businesses;
    }

}

and here's the Business.class:

@Table(database = DbFlowDataBase.class)
public class Business extends BaseModel {

    @PrimaryKey(autoincrement = true)
    public Integer id;

    @Column
    Integer userId;

}

Note dbFlowOneTwoManyUtilMethod is the one who makes all the magic.

Upvotes: 5

Related Questions