对象框删除实体并在另一个实体中删除 ToOne<> link 到它。应用程序现在仅适用于进行更改时的第一个构建变体

Objectbox delete Entity and remove ToOne<> link to it in another Entity. App now only works on first build variant when the change was made

在我的应用程序中,我有两个实体 CardEntityDeckCard。我想完全删除 CardEntity,并更新 DeckCard 以不再引用它。

public class CardEntity extends BaseEntity {

    private char letter;
    private int bonus;

    public CardEntity() {
    }

    public CardEntity(char letter) {
        this(letter, 0);
    }

    public CardEntity(char letter, int bonus) {
        this.letter = letter;
        this.bonus = bonus;
    }

    public char getLetter() {
        return letter;
    }

    public int getBonus() {
        return bonus;
    }

    public void setLetter(char letter) {
        this.letter = letter;
    }

    public void setBonus(int bonus) {
        this.bonus = bonus;
    }
}


@io.objectbox.annotation.Entity
public class DeckCard extends BaseEntity {

    private Integer position;
    private ToOne<CardEntity> card;

    public DeckCard() {
    }

    public DeckCard(Integer position, ToOne<CardEntity> card) {
        this.position = position;
        this.card = card;
    }

    public Integer getPosition() {
        return position;
    }

    public void setPosition(Integer position) {
        this.position = position;
    }

    public ToOne<CardEntity> getCard() {
        return card;
    }

    public void setCard(ToOne<CardEntity> card) {
        this.card = card;
    }
}

我想完全删除 CardEntity 并将 DeckCard 替换为

public class DeckCard extends BaseEntity {

    private Integer position;
    private char letter;
    private int bonus;

    public DeckCard() {
    }

    public DeckCard(Integer position, char letter, int bonus) {
        this.position = position;
        this.letter = letter;
        this.bonus = bonus;
    }

    public Integer getPosition() {
        return position;
    }

    public void setPosition(Integer position) {
        this.position = position;
    }

    public char getLetter() {
        return letter;
    }

    public void setLetter(char letter) {
        this.letter = letter;
    }

    public int getBonus() {
        return bonus;
    }

    public void setBonus(int bonus) {
        this.bonus = bonus;
    }
}

正如预期的那样工作正常。但是,它只适用于我当时在 Android Studio 中选择的任何 Build Variant。所谓选中,是指应用程序下次运行时更新 objectbox-models/default.json 文件。

如果我在 debug 构建变体上,那么它不会在 release 构建变体上工作,反之亦然。

我很确定我只需要在重命名或更改它的类型时使用 Uid 命令。在这种情况下,我什么都不做,我正在删除并创建新的东西。

下面是 objectbox 对 default.json` 文件创建的更改的包含 git 差异。

index 93401bf..85ef145 100644
--- a/core/objectbox-models/default.json
+++ b/core/objectbox-models/default.json
@@ -56,28 +56,8 @@
       "relations": []
     },
     {
-      "id": "3:6780553593369094070",
-      "lastPropertyId": "3:5487168684448628175",
-      "name": "CardEntity",
-      "properties": [
-        {
-          "id": "1:6704627251462640845",
-          "name": "id"
-        },
-        {
-          "id": "2:3991826258957414994",
-          "name": "letter"
-        },
-        {
-          "id": "3:5487168684448628175",
-          "name": "bonus"
-        }
-      ],
-      "relations": []
-    },
-    {
       "id": "5:5013495804474067017",
-      "lastPropertyId": "3:4210233198992719300",
+      "lastPropertyId": "5:3189718515123354120",
       "name": "DeckCard",
       "properties": [
         {
@@ -89,16 +69,19 @@
           "name": "position"
         },
         {
-          "id": "3:4210233198992719300",
-          "indexId": "1:4945633363495390935",
-          "name": "cardId"
+          "id": "4:256499331611759584",
+          "name": "letter"
+        },
+        {
+          "id": "5:3189718515123354120",
+          "name": "bonus"
         }
       ],
       "relations": []
     },
     {
       "id": "6:5212062744213571670",
-      "lastPropertyId": "2:2822761128112662405",
+      "lastPropertyId": "3:3928832151786754926",
       "name": "DeckEntity",
       "properties": [
         {
@@ -107,8 +90,11 @@
         },
         {
           "id": "2:2822761128112662405",
-          "indexId": "3:1461174128207782541",
-          "name": "date"
+          "name": "value"
+        },
+        {
+          "id": "3:3928832151786754926",
+          "name": "deckType"
         }
       ],
       "relations": [
@@ -362,10 +348,13 @@
   "retiredEntityUids": [
     4969708316960283445,
     5055976600245924974,
-    4563426853935656920
+    4563426853935656920,
+    6780553593369094070
   ],
   "retiredIndexUids": [
-    1897667010269871647
+    1897667010269871647,
+    1461174128207782541,
+    4945633363495390935
   ],
   "retiredPropertyUids": [
     5471484579006047884,
@@ -378,7 +367,11 @@
     6425939462895623468,
     6798215028749536251,
     3730774925178609899,
-    808949111095751851
+    808949111095751851,
+    4210233198992719300,
+    6704627251462640845,
+    3991826258957414994,
+    5487168684448628175
   ],
   "retiredRelationUids": [
     785742710602534748,

更新:使问题更加清晰...

更新:观察到的行为无法用当前版本的 ObjectBox(例如 2.5.1)重现,但用旧版本(2.0.0)重现。

总结:

  • 您打算"merge"将两个实体类型的属性合并为一个
  • CardEntity 已删除
  • DeckCard 获得两个额外的属性并将 ToOne 放到 CardEntity