简体   繁体   English

Gae Jdo坚持一对多拥有双向导航的关系

[英]Gae Jdo persistance on one-to-many owned relationship with bidirectional navigation

I'm trying to persist a one-to-many owned relationship with bidirectional navigation in GAE using JDO. 我正在尝试使用JDO在GAE中保持一对多拥有双向导航的关系。

I manually add the Contact to User class, and I would expect that in the end the Contact will have a reference to the parent User object. 我手动添加Contact to User类,我希望最终Contact将具有对父User对象的引用。

  • If I configure this manually before I persist the parent, I get an exception: org.datanucleus.store.appengine.DatastoreRelationFieldManager.checkForParentSwitch(DatastoreRelationFieldManager.java:204) 如果我在保留父节点之前手动配置它,我会得到一个异常: org.datanucleus.store.appengine.DatastoreRelationFieldManager.checkForParentSwitch(DatastoreRelationFieldManager.java:204)
  • After the User object persistence the parent reference is not updated. User对象持久性之后,父引用不会更新。
  • After the Contact object is retrieved from the datastore using the key the parent reference is not updated. 使用密钥从数据存储区检索Contact对象后,不会更新父引用。

I don't understand where my mistake is. 我不明白我的错误在哪里。

package test;

import java.util.ArrayList;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import com.google.appengine.api.datastore.Key;

public class DatastoreJdoTest extends LocalServiceTestCase {
    @Autowired
    @Qualifier("persistenceManagerFactory")
    PersistenceManagerFactory pmf;

    @Test
    public void testBatchInsert() {
        Key contactKey;
        PersistenceManager pm = pmf.getPersistenceManager();
        try {
            pm.currentTransaction().begin();
            User user = new User();
            Contact contact = new Contact("contact1");
            user.contacts.add(contact);

            /*
             * With this an exception is thrown
             * 
             * Detected attempt to establish User(1)/Contact(2) as the parent of
             * User(1) but the entity identified by User(1) has already been
             * persisted without a parent. A parent cannot be established or
             * changed once an object has been persisted.
             * org.datanucleus.store.appengine.FatalNucleusUserException:
             * Detected attempt to establish User(1)/Contact(2) as the parent of
             * User(1) but the entity identified by User(1) has already been
             * persisted without a parent. A parent cannot be established or
             * changed once an object has been persisted. at
             * org.datanucleus.store
             * .appengine.DatastoreRelationFieldManager.checkForParentSwitch
             * (DatastoreRelationFieldManager.java:204)
             */
            //contact.user = user;
            Assert.assertNull(contact.key);
            pm.makePersistent(user);
            Assert.assertNotNull(contact.key);

            pm.currentTransaction().commit();

            contactKey = contact.key;
            //this assertion is broken. why ? 
            //Assert.assertNotNull(contact.user);
        } finally {
            if (pm.currentTransaction().isActive()) {
                pm.currentTransaction().rollback();
            }
        }
        Contact contact2 = pm.getObjectById(Contact.class, contactKey);
        Assert.assertNotNull(contact2);
        //this assertion is broken. why the contact don't store the parent user ?
        Assert.assertNotNull(contact2.user);
    }
}

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
class User {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    public Key key;
    @Persistent
    public String name;
    @Persistent
    public List<Contact> contacts = new ArrayList<Contact>();
}

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
class Contact {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    Key key;
    @Persistent
    public String contact;
    @Persistent(mappedBy = "contacts", dependent = "true")
    public User user;

    public Contact(String contact) {
        this.contact = contact;
    }
}

According to App Engine docs , you should specify "mappedBy" in the owner of your relationship. 根据App Engine文档 ,您应该在关系的所有者中指定“mappedBy”。

You may also want to read Max Ross's article or to have a look at my code which accesses parent (Discussion) from a child object (Message) which is fetched from a query 您可能还想阅读Max Ross的文章或查看我的代码 ,该代码从查询中获取的子对象(Message)访问父(Discussion)

Just posting the code with the correction Dmitry pointed out for easier reading: 只需发布带有修正的代码,Dmitry指出更容易阅读:

import java.util.ArrayList;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import com.google.appengine.api.datastore.Key;

public class DatastoreJdoTest extends LocalServiceTestCase {
 @Autowired
 @Qualifier("persistenceManagerFactory")
 PersistenceManagerFactory pmf;

 @Test
 public void testBatchInsert() {
  Key contactKey;
  PersistenceManager pm = pmf.getPersistenceManager();
  try {
   pm.currentTransaction().begin();
   User user = new User();
   Contact contact = new Contact("contact1");
   user.contacts.add(contact);

   /*
    * With this an exception is thrown
    * 
    * Detected attempt to establish User(1)/Contact(2) as the parent of
    * User(1) but the entity identified by User(1) has already been
    * persisted without a parent. A parent cannot be established or
    * changed once an object has been persisted.
    * org.datanucleus.store.appengine.FatalNucleusUserException:
    * Detected attempt to establish User(1)/Contact(2) as the parent of
    * User(1) but the entity identified by User(1) has already been
    * persisted without a parent. A parent cannot be established or
    * changed once an object has been persisted. at
    * org.datanucleus.store
    * .appengine.DatastoreRelationFieldManager.checkForParentSwitch
    * (DatastoreRelationFieldManager.java:204)
    */
   //contact.user = user;
   Assert.assertNull(contact.key);
   pm.makePersistent(user);
   Assert.assertNotNull(contact.key);

   pm.currentTransaction().commit();

   contactKey = contact.key;
   //this assertion is broken. why ? 
   //Assert.assertNotNull(contact.user);
  } finally {
   if (pm.currentTransaction().isActive()) {
    pm.currentTransaction().rollback();
   }
  }
  Contact contact2 = pm.getObjectById(Contact.class, contactKey);
  Assert.assertNotNull(contact2);
  //this assertion is broken. why the contact don't store the parent user ?
  Assert.assertNotNull(contact2.user);
 }
}

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
class User {
 @PrimaryKey
 @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
 public Key key;
 @Persistent
 public String name;
 @Persistent(mappedBy = "user", dependent = "true")
 public List<Contact> contacts = new ArrayList<Contact>();
}

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
class Contact {
 @PrimaryKey
 @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
 Key key;
 @Persistent
 public String contact;
 @Persistent
 public User user;

 public Contact(String contact) {
  this.contact = contact;
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM