ORMlite Android 外键支持

发布于 2024-12-28 21:05:33 字数 552 浏览 1 评论 0原文

从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬シ愛 2025-01-04 21:05:33

AccountNameHolder 应针对表 Accounts 中的 DatabaseField 名称。如何做到这一点?

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

@DatabaseField(foreign = true)
private Account account;

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

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

此外,还有一些有关外部字段的示例代码

希望这有帮助。

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

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;

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文