I need to create folder structure using Room. I had 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 this.
@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 are working fine. but if I tried to move one folder to another folder, that means you need to change the parentId of the folder you are moving. When I do that, the update is failing without any exception.
so I ended up writing 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);
Above move, method is working as expected. But not sure, why @update is silently failing.