关于java:Room数据库编译错误:字段具有非唯一的列名

Room Database compile error: Field has non-unique column name

我在班级的某些字段上遇到此错误

error: Field has non-unique column name

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@Entity(tableName ="Team", foreignKeys = {
    @ForeignKey(entity = Group.class, parentColumns ="id", childColumns ="groupId")},
    indices = {@Index("groupId")})
public class Team {

    @PrimaryKey
    private long id;
    private long groupId;
    @SerializedName("Team")
    private String name;
    private String englishName;
    @SerializedName("Played")
    private int played;
    @SerializedName("Victories")
    private int win;
    @SerializedName("Draws")
    private int draw;
    @SerializedName("Defeats")
    private int defeat;
    @SerializedName("Made")
    private int goalFor;
    @SerializedName("Let")
    private int goalAgainst;
    @SerializedName("Diff")
    private int goalDiff;
    @SerializedName("Points")
    private int points;

    public Team() {

    }

    /* getter and setter methods */
}

例如,我在" win "," draw "和" groupId "上收到此错误。但不在" id "或" name "上。正如您所看到的,这是一个编译错误,除了标题中的那句话之外,它不再提供有关该错误的更多信息。

编辑:我尝试更改变量的名称,但是没有用。

编辑:" win "的Getter和setter方法,其他方法与此类似。

1
2
3
4
5
6
7
public int getWin() {
    return win;
}

public void setWin(int win) {
    this.win = win;
}


您需要添加一个前缀,以避免重复的列名。根据官方文档:

前缀
字符串前缀()
指定前缀,以在嵌入字段中添加字段的列名。

对于上面的示例,如果我们写了:

@Embedded(prefix ="foo_") Coordinates coordinates;

https://developer.android.com/reference/android/arch/persistence/room/Embedded.html#prefix()


我找到了解决方案(嗯,实际上不是解决方案)。
我还有一个称为"组"的实体:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Entity
public class Group {

    @PrimaryKey
    private long id;
    private String name;
    @Embedded
    private List<Team> teams;

    public Group() {

    }

    public Group(String name) {
        this.name = name;
    }

    /* getter and setter methods */

结果表明,带有"嵌入"批注的变量"团队"是我遇到的问题的根源。当我删除它时,代码工作正常。如果有人可以向我解释我做错了什么(或者我做了错?),我将不胜感激。

编辑:找到了与此问题相关的一些链接。

对于在库模块中定义的POJO的@NonNull注释的构造函数参数,Android Room @Embedded注释编译失败

https://github.com/googlesamples/android-architecture-components/issues/318