android - 更新外部对象 ORMLite - ANDROID

标签 android sql-update foreign-key-relationship cascade ormlite

我想知道如何更新 ORMLITE 中的外来对象?

假设我有这些类(class)

这是父级

public static class Parent {

    @DatabaseField(generatedId=true,allowGeneratedIdInsert=true)
    int id;
    @DatabaseField
    String name;
    ...

这是 child

public static class Child{
    @DatabaseField(generatedId=true,allowGeneratedIdInsert=true)
    int id;
    @DatabaseField(canBeNull = false, foreign = true,foreignAutoCreate = true,foreignAutoRefresh = true, columnDefinition = "integer references parent(id) on update cascade")
    Parent parent;
    ...

假设我们有这些值:

给家长 id=5 名字=“大”

对于 child 编号=338 父级 = {id=5,name="big"}

在这里,当我想更新我的 parent ID 时,它运行良好:

firstId=5, lastId=6

UpdateBuilder<Parent, Integer> builder = ParentDao.updateBuilder();
builder.where().eq("id", firstId);
builder.updateColumnValue("id", lastId);
builder.update();

之后,我使用 select 命令来确定它是否已更新。我确定它正在为 Parent 更新。但是当我更新我的 Parent id 时,我在我的 Child 对象中丢失了父对象。它看起来像这样:

给家长 id=6 名字=“大”

对于 child 编号=338 父级 = {null}

有人知道这个的解决方案吗?

最佳答案

OrmLite 不会像其他 ORM 一样自动自动保存嵌套对象。

因此您还需要更新 Child。


步骤是

  1. 首先你得到父对象。
  2. 从那里读 child 。
  3. 更新 parent
  4. 现在将更新的父项设置为子项
  5. 也更新 child 。

关于android - 更新外部对象 ORMLite - ANDROID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19174874/

相关文章:

mysql - SQL:从复杂的选择中更新

sql - DB2 等效于 T-SQL 的输出子句

sql - 使用 MS Access SQL 查询创建与现有表具有一对一关系的第二个表

python - 如何从一组子实例中获取所有相关/父实例而不循环遍历后一组

mysql - 外键级联更新会传播所有可能的列吗?

android - 如何增加 RecyclerView 上当前焦点项目的大小?

java - android sdk,错误 : java. lang.SecurityException:权限拒绝:尝试更改组件状态

Mysql 使用 IN 查询强制更新索引

android - 无法在 Android 手机中安装 Cordova 应用程序

Android 4.4 BLE指示数据大于20字节