ORMlite Android 外键支持
从 ORMlite 文档来看,我并不聪明。是否可以在类中声明该参数是外键?
例如,我有表 Customer:
@DatabaseTable(tableName = "customer")
public class Customer {
@DatabaseField(id = true)
private String customerName;
@DatabaseField
private String customerSurname;
@DatabaseField(foreign = true)
private String accountNameHolder;
@DatabaseField
private int age;
public Customer() {
}
}
AccountNameHolder 应该针对表 Accounts 中的 DatabaseField 名称。怎么做呢?我只找到了参数foreign = true,但是没有任何关于它代表哪个参数以及来自哪个表的信息。
谢谢
I am not clever from ORMlite documentation. Is is possible to declare in class, that this parameter is foreign key?
e.g. I have table Customer:
@DatabaseTable(tableName = "customer")
public class Customer {
@DatabaseField(id = true)
private String customerName;
@DatabaseField
private String customerSurname;
@DatabaseField(foreign = true)
private String accountNameHolder;
@DatabaseField
private int age;
public Customer() {
}
}
AccountNameHolder should aim towards DatabaseField name from table Accounts. How to do that? I have found only parameter foreign = true, but there is nothing about, which parameter and from which table it represents.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太确定您想要什么,但可能您应该将外部字段更改为实际的类型而不是名称:
内部,ORMLite 将在
Customer
表中存储account_id
字段(可能是字符串名称),但您不必担心这一点。请记住,当您查询Customer
时,在account
字段上设置的Account
将仅设置 id 字段。要让 ORMLite 也查找帐户,您需要设置foreignAutoRefresh=true
。正如@Lalit 指出的,这里有一些关于这个主题的文档。我们在文档上花了很长时间,所以它应该会有所帮助。
此外,还有一些有关外部字段的示例代码。
希望这有帮助。
I'm not exactly sure what you want but possibly you should change your foreign field to be the actual type instead of a name:
Internally, ORMLite will store a
account_id
field (maybe the string name) in theCustomer
table but you don't have to worry about that. Remember that when you query for aCustomer
, theAccount
that is set on theaccount
field will just have the id field set. To have ORMLite also lookup the account you will need to set theforeignAutoRefresh=true
.As @Lalit pointed out, here is some documentation on this subject. We've spent a long time on the documentation so it should be helpful.
Also, there is some example code about foreign fields.
Hope this helps.