ORMlite Android foreign key support

我的梦境 提交于 2019-12-18 12:53:10

问题


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


回答1:


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.

  • Foreign objects
  • Foreign auto refresh

Also, there is some example code about foreign fields.

Hope this helps.



来源:https://stackoverflow.com/questions/9002519/ormlite-android-foreign-key-support

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!