简体   繁体   中英

How make relationship between two entities - Sugar ORM

I use Sugar ORM in my Android app and I would like to know how to make relationship between entities. The thing I would like to is to have more emails and phone numbers (work, personal, home)

I have tried this:

I can add it like this..

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

but when I want to load, I have an error. Class cannot be read from Sqlite3 database.

Contact

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;
    }
}

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; }
}

Generally, you should be initialized Contact before find or update this. Can be Contact have method getName() is null.

In this case, Model.Contact.getName() is a null object reference.

So, you should check my code:

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

OR

You should tried with code to load all contacts, this seem like findById but it get all rows in Contact :

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

In the debugger Android Studio, please watch all values. Can be any incorrect in your data.

You will need whats known as a linker class as Sugar has no concept of one to one or one to many or many to many relationships (lists and objects cannot be stored in sugar). Only strings, ints etc:

    public class ContactItem extends SugarRecord {

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

         String content;
         Integer type;
         Contact contact;

        }

When you first create the objects:

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

when you get from DB you should get all the Contact objects you need and fetch it's objects this way:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM