Stepan Stepan - 4 months ago 32
Android Question

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

Answer

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.