Error updating using self-reflective foreign key with number update method

I need to create a folder structure using Room . I have followed this approach.

@Entity(foreignKeys = @ForeignKey(entity = NodeEntity.class,
        parentColumns = "id",
        childColumns = "parentId", onDelete = CASCADE), indices = {@Index(value = {"parentId"})})
public class NodeEntity {
    @PrimaryKey
    @NonNull
    private String id;
    private int isLeafItem;
    private int level;
    private String name;
    private String parentId;
    private double price;
    private int quantity;
    private String tags;
    @TypeConverters(DateConverter.class)
    private Date createdAt;
}

My Node DAO is like that.

@Dao
public interface NodeDao {

    @Insert(onConflict = REPLACE)
    void createNode(NodeEntity nodeEntity);

    @Query("SELECT * FROM NodeEntity WHERE parentId = :parentId ")
    Flowable<List<NodeEntity>> getNodesByParentId(String parentId);

    @Query("SELECT * FROM NodeEntity WHERE level = :level")
    Flowable<List<NodeEntity>> getNodesByLevel(int level);

    @Query("SELECT * FROM NodeEntity WHERE id = :nodeId")
    Single<NodeEntity> getNode(String nodeId);

    @Update(onConflict = REPLACE)
    void updateNode(NodeEntity node);

    @Delete
    void deleteNode(NodeEntity nodeEntity);
}

All operations work fine. but if I tried to move one folder to another folder, it means that you need to change the parentId of the folder you are moving to. When I do this, the update is interrupted without any exception.

so I wrote a separate method like this.

@Query("UPDATE NodeEntity SET parentId = :newParentId, level=:newLevelId where id=:id AND parentId=:parentId")
    void moveNode(String newParentId,int newLevelId, String id, String parentId);

The method works as expected on the move. But not sure why @update silently fails.

0
source share

No one has answered this question yet.


All Articles