ORMlite Android 外键支持 [英] ORMlite Android foreign key support

查看:22
本文介绍了ORMlite Android 外键支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ORMlite 文档中并不聪明.是否可以在类中声明这个参数是外键?

I am not clever from ORMlite documentation. Is is possible to declare in class, that this parameter is foreign key?

例如我有表客户:

@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,但没有任何关于它代表哪个参数和来自哪个表的信息.

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.

谢谢

推荐答案

AccountNameHolder 应针对表 Accounts 中的 DatabaseField 名称.该怎么做?

AccountNameHolder should aim towards DatabaseField name from table Accounts. How to do that?

我不确定您想要什么,但您可能应该将您的外部字段更改为实际的 type 而不是名称:

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:

@DatabaseField(foreign = true)
private Account account;

在内部,ORMLite 将在 Customer<中存储一个 account_id 字段(可能是字符串名称)/code> 表,但您不必担心.请记住,当您查询 Customer 时,在 account 字段上设置的 Account 只会设置 id 字段.要让 ORMLite 也查找帐户,您需要设置 foreignAutoRefresh=true.

Internally, ORMLite will store a account_id field (maybe the string name) in the Customer table but you don't have to worry about that. Remember that when you query for a Customer, the Account that is set on the account field will just have the id field set. To have ORMLite also lookup the account you will need to set the foreignAutoRefresh=true.

正如@Lalit 所指出的,这里有一些关于这个主题的文档.我们在文档上花了很长时间,所以它应该会有所帮助.

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.

另外,还有一些关于外部字段的示例代码.

希望这会有所帮助.

这篇关于ORMlite Android 外键支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆