1

我正在尝试将 DBFlow 与我有一对多关系的对象一起使用(用户有很多企业),我在他们的官方 wiki 上关注教程,但它只是不想工作。

这是我的用户:

@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;

和我的生意

@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;

我应该像 associateBusinessWithUser 那样在 Business 中制定方法吗?或者我应该如何链接这个?

4

1 回答 1

5

这是 dbFlowVersion = '3.0.0-beta1' 的代码。我简化了你的课程:

    @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;
    }

}

这是Business.class:

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

    @PrimaryKey(autoincrement = true)
    public Integer id;

    @Column
    Integer userId;

}

注意 dbFlowOneTwoManyUtilMethod 是创造所有魔力的方法。

于 2016-01-14T13:05:43.970 回答