java - 房间一对多警告

标签 java android android-room

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

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 中:

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

下面是我的 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;
    }
}

很可能我还没有完全掌握 Room 一对多关系的概念,但是我到底做错了什么并收到该警告?

最佳答案

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

像这样更改您的代码:

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

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

关于java - 房间一对多警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60503727/

相关文章:

java - 如何为 URL 创建网络图?

java - Spring错误创建名称为“org.springframework.aop.config.internalAutoProxyCreator”的bean

android - 如何通过罗盘读数和陀螺仪读数获得手机的方位角?

android - 使用协程更新 UI 异步调用

android - 在 `runBlockingTest` 测试 Room 的事务查询

android - Field 的模糊 setter/getter ...房间持久性库

java - 如何以编程方式在 eclipse 中添加我自己的 checkstyle?

java - Netbeans 部署 JavaFX 应用管理员权限

android - Flutter:使用 InteractiveViewer 双击启用图像放大/缩小

java - 删除不适用于 HTC 的通话记录