保存与ORMLite在Android嵌套异物嵌套、异物、ORMLite、Android

2023-09-14 00:07:57 作者:天黑路滑社会人渣

在Android上工作时,并ORMLite既节省了浅层次的对象?我有一个数据结构嵌套的对象,这两者都是新成立的,我想能够保存他们两个人有一个调用dao.create()

有关〔实施例,我有以下的父类。

  @DatabaseTable
公共类父{

  @DatabaseField(generatedId =真)
  公众诠释ID;

  @DatabaseField
  公共字符串名称;

  @DatabaseField
  公共孩童;
}
 

和下面的子类。

  @DatabaseTable
公共类儿童{

  @DatabaseField(generatedId =真)
  公众诠释ID;

  @DatabaseField
  公共字符串名称;
}
 
Android DB那些事 数据库加密

我希望能够做到以下几点。

 父父=新的父();
parent.name =ParentName;

孩童=新的儿童();
child.name =ChildName;

parent.child =子女;

// ..获得帮助,并创建DAO对象...
dao.create(父);
 

在此过程中,父对象持久化,但不是孩子对象自动生成的 child_id 列在父表设置为0,这是正常的行为?有没有办法让嵌套的对象持久化和传播的主键了?

解决方案   

在此过程中,父对象持久化,但不是孩子的对象,并在父表被设置为0的自动生成child_id列这是正常的行为?

OrmLite不会自动保存嵌套对象自动将像其他奥姆斯。它的目的是利用其名称中的精简版的部分认真,充分考虑了KISS的座右铭。

不过,你可以很容易地嵌套对象通过创建子工作的在的创建父。

 父父=新的父();
parent.name =ParentName;

孩童=新的儿童();
child.name =ChildName;

parent.child =子女;

//这将更新子ID
childDao.create(子);

//这节省母体与孩子的id
parentDao.create(父);
 

还有一点要注意的是,当你查询父对象,那你回来的子对象的只有的是它的id字段检索。如果id是自动生成的INT(例如),那么上面的名称字段将不被检索,直到你做的子对象的最新情况。

  //假设家长的ID名称
父父= parentDao.queryForId(ParentName);
的System.out.println(子ID应设置:+ parent.child.id);
的System.out.println(孩子的名字应为空:+ parent.child.name);

//现在我们刷新子对象加载所有字段
childDao.refresh(parent.child);
的System.out.println(孩子的名字,现在应该设置:+ parent.child.name);
 

有关此更多的文档,请参阅有关外来物的在线页面场。

When working on Android, does ORMLite only save shallow level objects? I have a data structure with nested Objects, both of which are newly created, and I would like to be able to save both of them with one call to dao.create()

For exmaple, I have the following Parent Class.

@DatabaseTable
public class Parent {

  @DatabaseField(generatedId=true)
  public int id;

  @DatabaseField
  public String name;

  @DatabaseField
  public Child child;
}

and the following Child Class.

@DatabaseTable
public class Child {

  @DatabaseField(generatedId=true)
  public int id;

  @DatabaseField
  public String name;
}

I want to be able to do the following.

Parent parent = new Parent();
parent.name = "ParentName";

Child child = new Child();
child.name = "ChildName";

parent.child = child;

//  .. get helper and create dao object...
dao.create(parent);

When doing this, the parent object is persisted but not the child object and the auto-generated child_id column in the parent table is set to 0. Is this normal behavior? Is there a way to have nested objects persisted and propagate the primary key up?

解决方案

When doing this, the parent object is persisted but not the child object and the auto-generated child_id column in the parent table is set to 0. Is this normal behavior?

OrmLite does not auto-save nested objects automagically like other ORMs. It is designed taking the "Lite" part of its name seriously and with the KISS motto in mind.

However, you can easily get nested objects to work by creating the child before you create the parent.

Parent parent = new Parent();
parent.name = "ParentName";

Child child = new Child();
child.name = "ChildName";

parent.child = child;

// this will update the id in child
childDao.create(child);

// this saves the parent with the id of the child
parentDao.create(parent);

One more thing to note is that when you query for a Parent object, the child object that you get back only has its id field retrieved. If the id is an auto-generated int (for example), then the above name field will not be retrieved until you do an update on the child object.

// assuming the id of the Parent is the name
Parent parent = parentDao.queryForId("ParentName");
System.out.println("Child id should be set: " + parent.child.id);
System.out.println("Child name should be null: " + parent.child.name);

// now we refresh the child object to load all of the fields
childDao.refresh(parent.child);
System.out.println("Child name should now be set: " + parent.child.name);

For more documentation about this, see the online page about Foreign Object Fields.

 
精彩推荐
图片推荐