如何使两个实体之间的关系 - 糖ORM

时间:2016-07-21 16:46:55

标签: android database orm android-recyclerview

我在Android应用中使用 Sugar ORM ,我想知道如何在实体之间制作 关系。我想要的是拥有更多的电子邮件和电话号码(工作,个人,家庭)

我试过这个:

我可以像这样添加..

Contact contact = new Contact();
ContactItem contactItem = new ContactItem();
contactItem.setType(1);
contact.getItems.add(contactItem);

但是当我想加载时,我有一个错误。无法从Sqlite3数据库中读取类。

联系

public class Contact extends SugarRecord {

    private String name;
    private String phoneNumber;
    private String email;
    private boolean favourite;
    private String imagePath;
    @Ignore private boolean visibleFirstLetter;

    public List<ContactItem> items;

    public Contact() {
        //empty constructor
    }

    public Contact(String name, String number, String email, boolean favourite, String imagePath) {
        this.name = name;
        this.phoneNumber = number;
        this.email = email;
        this.favourite = favourite;
        this.imagePath = imagePath;
        this.items = new ArrayList<>();
    }

    public void setName(String name) {this.name = name; }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public void setFavourite(Boolean favourite) {
        this.favourite = favourite;
    }

    public void setVisibleFirstLetter(Boolean visibleFirstLetter) {
        this.visibleFirstLetter = visibleFirstLetter;
    }
    public String getName() {
        return name;
    }

    public String getEmail() { return email; }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public boolean isFavourite() { return favourite; }

    public boolean isVisibleFirstLetter() {
        return visibleFirstLetter;
    }

    public String getImagePath() { return imagePath; }

    public void setImagePath(String imagePath) { this.imagePath = imagePath; }

    public List<ContactItem> getItems() {
        return items;
    }
}

产品

public class ContactItem {

    String content;
    Integer type;
    Contact contact;


    public static final Integer HOME = 0;
    public static final Integer WORK = 1;
    public static final Integer EMAIL = 2;
    public static final Integer FAX = 3;

    public ContactItem() {
        //empty constructor
    }
    public ContactItem(Integer type, String content) {
        this.type = type;
        this.content = content;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getContent() { return content; }

    public Integer getType() { return type; }

    public Contact getContact() { return contact; }

    public void setContact(Contact contact) { this.contact = contact; }
}

2 个答案:

答案 0 :(得分:1)

通常,在查找或更新此内容之前,应初始化Contact。 可以Contact方法getName()为空。

在这种情况下,Model.Contact.getName()是一个空对象引用。

所以,你应该检查我的代码:

Contact contact = new Contact("Stepan", "09293293", stepan.stack@gmail.com, true);
contact.save();
Contact contact = SugarRecord.findById(Contact.class, (long) 1);

您应尝试使用代码加载所有联系人,这看起来像findById,但它会获得Contact中的所有行:

List<Contact> contacts = SugarRecord.listAll(Contact.class);

在调试器Android Studio中,请观看所有值。可能是您的数据不正确。

答案 1 :(得分:0)

你将需要什么称为链接器类,因为Sugar没有一对一或一对多或多对多关系的概念(列表和对象不能存储在糖中)。只有字符串,整数等:

    public class ContactItem extends SugarRecord {

        @Column(name = "contact_linker_id", unique = true)
        @Expose
        private int mContactId;

         String content;
         Integer type;
         Contact contact;

        }

首次创建对象时:

Contact contact = new Contact();
contact.getId() //or other unique identifier
ContactItem contactItem = new ContactItem();
contactItem.mContactId = contact.getId();
contactItem.save();
contact.save();

当你从DB获得时,你应该得到你需要的所有Contact对象并以这种方式获取它的对象:

Contact contact = Select.from(Contact.class).first();
ContactItem contactItem = Select.from(ContactItem.class).where("contact_linker_id").eq(contact.getId());
contact.addItem(contactItem);