更新外部对象ORMLite-ANDROID
我想知道如何更新ORMLITE中的外来对象 假设我有这些课程 这是家长更新外部对象ORMLite-ANDROID,android,sql-update,foreign-key-relationship,cascade,ormlite,Android,Sql Update,Foreign Key Relationship,Cascade,Ormlite,我想知道如何更新ORMLITE中的外来对象 假设我有这些课程 这是家长 public static class Parent { @DatabaseField(generatedId=true,allowGeneratedIdInsert=true) int id; @DatabaseField String name; ... public static class Child{ @DatabaseField(generatedId=true
public static class Parent {
@DatabaseField(generatedId=true,allowGeneratedIdInsert=true)
int id;
@DatabaseField
String name;
...
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;
...
这是孩子
public static class Parent {
@DatabaseField(generatedId=true,allowGeneratedIdInsert=true)
int id;
@DatabaseField
String name;
...
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
name=“大”
儿童版
id=338
父项={id=5,name=“big”}
在这里,当我想更新我的家长id时,它工作正常:
firstId=5,lastId=6
UpdateBuilder<Parent, Integer> builder = ParentDao.updateBuilder();
builder.where().eq("id", firstId);
builder.updateColumnValue("id", lastId);
builder.update();
UpdateBuilder builder=ParentDao.UpdateBuilder();
builder.where().eq(“id”,firstId);
builder.updateColumnValue(“id”,lastId);
builder.update();
之后,我使用select命令来确保它是否被更新。我确信它正在为家长更新。但当我更新我的父id时,我的子对象中丢失了父对象。它是这样出现的:
用于家长
id=6
name=“大”
儿童版
id=338
父项={null}
有人知道解决这个问题的办法吗 OrmLite不会像其他ORM一样自动保存嵌套对象。
所以您还需要更新Child
步骤是
child.parent
字段:
// child has a parent of #5
... child.getParent();
// update it with a new parent #6
Parent parent6 = new Parent(...);
// create the parent first so it gets an id
parentDao.create(parent6);
// set it on the child's field
child.setParent(parent6);
childDao.update(child);
这将更新数据库中的child
字段。父项的id
字段将从5更新为6
如果需要将父id字段从5直接更新到6,则需要刷新任何现有的子对象:
childDao.refresh(child);
更新父项后如何更新子项?我不能使用where命令,因为在child中,parent为null。你们有这样的例子吗?我想Emre问的是更新级联,而不是嵌套对象,我的意思是他问的是数据库功能,比如用于him@MohammedSubhiSheikhQuroush我所说的:)我也回答了同样的问题。不是吗?事实上,更新id的想法是完全错误的,id不应该更新,这样如果Emre不更新id,他就不会有问题了id@MohammedSubhiSheikhQuroush:)我没有说过要更新id。ORMlite不支持级联插入/更新/删除。你必须管理这些。这就是我在这里添加步骤的原因。。