Room one-to-many warning

余生颓废 提交于 2020-03-16 18:08:49

问题


When creating my contact DAO and related classes, I am getting the following error:

The query returns some columns [mContactId, mAddress, mPostcode, mCity, mCountry, mAddressType]
 which are not used by org.linphone.contacts.managementWS.ContactWithAddresses. You can use 
@ColumnInfo annotation on the fields to specify the mapping. 
org.linphone.contacts.managementWS.ContactWithAddresses has some fields [mName, mSurname, 
mFullName, mCompany, mNote, mIsBlocked] which are not returned by the query. If they are not 
supposed to be read from the result, you can mark them with @Ignore annotation. You can suppress 
this warning by annotating the method with @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH). 
Columns returned by the query: id, mContactId, mAddress, mPostcode, mCity, mCountry, 
mAddressType. Fields in org.linphone.contacts.managementWS.ContactWithAddresses: id, mName, 
mSurname, mFullName, mCompany, mNote, mIsBlocked.

In my ContactsDao:

@Query("SELECT * FROM contacts_table")
    List<Contact> getAll();

    @Transaction
    @Query("SELECT * FROM phone_numbers_table")
    List<ContactWithNumbers> getContactsWithPhoneNumbers();

ContactsWithNumbers.java:

@Embedded public Contact contact;

    @Relation(parentColumn = "id", entityColumn = "mContactId", entity = PhoneNumbers.class)
    public List<PhoneNumbers> numbers;

And below is my Contact.java:

@Entity(tableName = "contacts_table")
public class Contact {

    // TODO - members should be private, not public. Changed to workaround error.

    @PrimaryKey(autoGenerate = true)
    public int id;

    /* String resource ID for the user name */
    @SerializedName("first_name")
    public String mName;
    /* String resource ID for the user surname */
    @SerializedName("last_name")
    public String mSurname;
    /* String resource ID for the user's full name */
    @SerializedName("full_name")
    public String mFullName;
    /* String resource ID for the user company */
    @SerializedName("company")
    public String mCompany;
    /* String resource ID for the user's phone number(s) */
    /** String resource ID for the user's note */
    @SerializedName("note")
    public String mNote;

    @SerializedName("blocked")
    public boolean mIsBlocked;

    /**
     * @param firstName
     * @param lastName
     * @param fullName
     * @param company
     * @param note
     * @param isBlocked
     */
    @Ignore
    public Contact(
            String firstName,
            String lastName,
            String fullName,
            String company,
            String note,
            boolean isBlocked) {
        super();
        this.mName = firstName;
        this.mSurname = lastName;
        this.mFullName = fullName;
        this.mCompany = company;
        this.mNote = note;
        this.mIsBlocked = isBlocked;
    }

    public Contact(String name, String surname, String company, String note, boolean isBlocked) {
        this.mName = name;
        this.mSurname = surname;
        this.mCompany = company;
        this.mNote = note;
        this.mIsBlocked = isBlocked;
    }

    public int getId() {
        return id;
    }

    public String getmName() {
        return mName;
    }

    public String getmSurname() {
        return mSurname;
    }

    public String getmFullName() {
        return mName + " " + mSurname;
    }

    public String getmCompany() {
        return mCompany;
    }

    public String getmNote() {
        return mNote;
    }

    public boolean getmIsBlocked() {
        return mIsBlocked;
    }
}

It is quite likely that I have not fully grasped the concept of Room one-to-many relations, but what exactly am I doing wrong here and getting that warning?


回答1:


It's say very clear: You can use @ColumnInfo annotation on the fields to specify the mapping.

Change your code like this:

@NonNull
@PrimaryKey
@ColumnInfo(name = "id")
private String id;

More at codelab: https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#0



来源:https://stackoverflow.com/questions/60503727/room-one-to-many-warning

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