如何使用 ORMLite 实现两个类之间的关系?我知道外国字段,但我无法将非字符串部门字段添加到产品类
类产品
@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在代码中也可能有帮助。
关于java - ORMLite android类字段是其他类的id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35426879/