房间一对多警告 [英] Room one-to-many warning

查看:378
本文介绍了房间一对多警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建联系人DAO和相关类时,出现以下错误:

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.

在我的ContactsDao中:

In my ContactsDao:

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

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

ContactsWithNumbers.java:

ContactsWithNumbers.java:

@Embedded public Contact contact;

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

下面是我的Contact.java:

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?

推荐答案

说得很清楚:您可以在字段上使用@ColumnInfo批注指定映射.

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

像这样更改您的代码:

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

有关代码实验室的更多信息: https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#0

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

这篇关于房间一对多警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆