首页 > 解决方案 > 如何参考尚未持久化子对象的子对象(Objectify)首先保存父实体?

问题描述

有没有办法在方法中创建子实体的引用,但在不同的方法中保留子实体?

现在,我首先创建引用,然后以相同的方法保留子项。只有在完成此操作后,父级才会被持久化。如果在保存父实体期间发生 DatastoreTimeoutException、ConcurrentModificationException 或 DatastoreFailureException,我会留下子实体持久保存在数据存储区中,这是没有用的。

@Entity
@Index
public class parentEntity
    {

@Id
private String uniqueIdentifier;
private String name = new String();
@Load
private LinkedList <Ref <childEntity>> refToChild = new LinkedList <Ref <childEntity>>();

          }

@Entity
@Index
public class childEntity
    {

@Id
private String uniqueIdentifier;
private String address = new String();
@Load
@Parent
public Ref <parentEntity> refToParent;

          }
This method first creates the reference parent has with child and then persists the entity(child)
public static void createRefAndSaveChildEntity()
{
   Ref.create( key ) ;//creates the reference with child
   ofy().save().entity( childEntity ).now(); 
}       

我只想在父母(参考孩子)被坚持之后才坚持孩子。

标签: javagoogle-cloud-datastoreobjectify

解决方案


推荐阅读