ORMLite android class 字段是其他 class 的 id

ORMLite android class field is id of other class

如何使用 ORMLite 实现 2 classes 之间的关系?我知道外国字段,但我无法将非字符串部门字段添加到产品 class

Class 产品

@DatabaseTable(tableName = "PRODUCTS")
public class Product {

    @DatabaseField(id = true)
    private String id;

    @DatabaseField()
    private String name;

    @DatabaseField() //This field is id of Department class
    private String department;

部门class

@DatabaseTable(tableName = "DEPARTMENTS")
public class Department {

    @DatabaseField(id = true)
    private String id;

    @DatabaseField()
    private String name;

How to implement relationship between 2 classes using ORMLite? I know about foreign field, but i can't add non-string Department field to product class

RTFM 请。您是否看过任何文档或示例?这是 docs on foreign objects。您可以清楚地看到,您将 Department 放入 Product 而不是 String。文档显示 Order 对象具有 Account 字段。

@DatabaseField
private Department department;

在幕后,ORMLite 所做的实际上只是将部门的 id 存储到您的 Product 中。如果您真的查看架构,那么您会在那里看到一个字符串,但您自己不会这样做。

然后当您检索 Product 时,它将有一个 Department 字段,但只会填写 id

代码中还有一个 foreign object example 可能也有帮助。