为什么我拥有的一对多关系不会持久保存到GAE数据存储区?

时间:2012-01-16 04:02:13

标签: java google-app-engine google-cloud-datastore jdo datanucleus

我定义了两个类并映射到GAE数据存储区 - 一个Person类和一个LocationStamp类,它表示单个纬度/经度组合以及时间戳。 PersonLocationStamp之间存在一对一的映射,与a forum post on one-to-many relationships类似的行实现。

Person的DAO中,我有以下方法:

   public LocationStamp addLocationStampForCurrentUser(LocationStamp ls)
   {
      PersistenceManager pm = getPersistenceManager();

      Person p = getProfileForCurrentUser();
      p.getLocationStamps().add(ls);

      pm.currentTransaction().begin();
      try {
         pm.makePersistent(p);
         pm.currentTransaction().commit();
         return ls;
      } finally {
         if (pm.currentTransaction().isActive()) {
            pm.currentTransaction().rollback();
         }
         pm.close();
      }
   }    

当我尝试使用此方法向对象的LocationStamp实体集合添加条目时,关系不会被持久化。稍后对相关Person对象的查询将返回带有电子邮件地址的正确对象,但locationStamps列表为空。此外,App Engine服务器的数据查看器未显示任何LocationStamp实体(并且locationStamps表中没有为Person显示任何列。

我仔细按照论坛帖子中的说明操作,但我不确定我是否遗漏了某些内容。

以下是我的实体:

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class Person
{

   @PrimaryKey
   @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
   private Key key;

   @Persistent
   private String emailAddress;

   @Persistent
   private String name;

   @Persistent(mappedBy = "person")
   @Element(dependent = "true")
   @Order(extensions = @Extension(vendorName = "datanucleus", key = "list-ordering", value = "timestamp desc"))
   private final List<LocationStamp> locationStamps = new ArrayList<LocationStamp>();

   // ... getters and setters ...
}

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class LocationStamp
{

   @PrimaryKey
   @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
   private Key key;

   @Persistent
   private double latitude;

   @Persistent
   private double longitude;

   @Persistent
   private Date timestamp;

   @Persistent
   private boolean automatic;

   @Persistent
   private Person person;

   // ... getters and setters ...
}

由于它们已在上述代码中使用,因此以下是其他几种方法的定义:

   public Person getProfileForCurrentUser()
   {
      PersistenceManager pm = getPersistenceManager();
      try {
         Key k = getKeyForEmailAddress(getCurrentUserEmail());
         return pm.getObjectById(Person.class, k);
      } catch (JDOObjectNotFoundException e) {
         // Create a new profile if the new one isn't found
         return updateProfileForCurrentUser(new Person());
      } finally {
         pm.close();
      }
   }

   public Person updateProfileForCurrentUser(Person p)
   {
      p.setEmailAddress(getCurrentUserEmail());
      return update(p);
   }

   public Person update(Person p)
   {
      PersistenceManager pm = getPersistenceManager();
      try {
         pm.currentTransaction().begin();
         Key k = getKeyForEmailAddress(p.getEmailAddress());
         p.setKey(k);
         pm.makePersistent(p);
         pm.currentTransaction().commit();
         return p;
      } finally {
         if (pm.currentTransaction().isActive()) {
            pm.currentTransaction().rollback();
         }
         pm.close();
      }
   }

   private Key getKeyForEmailAddress(String emailAddress)
   {
      return KeyFactory.createKey(Person.class.getSimpleName(), emailAddress);
   }

   private static PersistenceManager getPersistenceManager()
   {
      // PMF is a singleton class that returns an instance of PersistenceManagerFactory
      return PMF.get().getPersistenceManager();
   }

1 个答案:

答案 0 :(得分:1)

当Persistence对象中有List时,列表的加载是一个惰性操作。因此,如果在加载列表之前关闭PersistenceManager,则不会加载列表。

尝试使用:

public Person getProfileForCurrentUser()
{
    PersistenceManager pm = getPersistenceManager();
    try {
        Key k = getKeyForEmailAddress(getCurrentUserEmail());
        Person p = pm.getObjectById(Person.class, k);
        p.getLocationStamps().size();
        return p;
    } catch (JDOObjectNotFoundException e) {
        // Create a new profile if the new one isn't found
        return updateProfileForCurrentUser(new Person());
    } finally {
        pm.close();
    }
}