【问题标题】:Bidirectional one to one mapping in GAE using JDO?使用JDO在GAE中进行双向一对一映射?
【发布时间】:2014-08-24 18:20:37
【问题描述】:

如何使用Google Application Engine (GAE) 和Java Data Objects (JDO) 实现双向一对一映射?

我有一个包含contactInfo 对象的User 类和一个包含user 对象的ContactInfo

@PersistenceCapable(identityType ="APPLICATION", detachable = "true")
public class User{
  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Long id;

  private String name;

  @Persistent(dependent = "true")
  private ContactInfo child;

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public ContactInfo getChild() {
    return child;
  }

  public void setChild(ContactInfo child) {
    this.child = child;
  }
}


@PersistenceCapable(identityType ="APPLICATION", detachable = "true")
public class ContactInfo {
  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Key id;

  @Persistent(mappedBy = "child")
  private User parent;

  private String contactDetail;

  public Key getId() {
    return id;
  }

  public void setId(Key id) {
    this.id = id;
  }

  public String getContactDetail() {
    return contactDetail;
  }

  public void setContactDetail(String contactDetail) {
    this.contactDetail = contactDetail;
  }
}

我在从 API 资源管理器测试 API 时遇到以下错误

com.google.appengine.repackaged.org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.demo.jdo.ContactInfo[\"user\"]->com.demo.jdo.User[\"contactInfo\"]->com.demo.jdo.ContactInfo[\"user\"]-

【问题讨论】:

    标签: rest google-app-engine google-cloud-datastore jdo


    【解决方案1】:

    标准 JDO 1-1 bidir 简单地从http://www.datanucleus.org/products/accessplatform_3_1/jdo/orm/one_to_one.html#bi 找到 GAE 在这方面应该没有什么不同。我上次使用它时(可能是 3 年前),他们进行了一些测试,想想下面的那些 http://code.google.com/p/datanucleus-appengine/source/browse/#svn%2Ftrunk%2Ftests%2Fcom%2Fgoogle%2Fappengine%2Fdatanucleus

    您的问题没有提供您在注释方面尝试过的定义

    【讨论】:

    • 为什么要在两端加上“mappedBy”?只有一端
    • 我应该放在哪一端?除此之外代码有什么问题??
    • 在非所有者方面。提供的链接解释了这一点。
    • 感谢比利,找到上面发布的解决方案作为答案。
    【解决方案2】:

    找到了解决方案,问题在于错误使用 mappedBy 以及子对象中存在 getter 和 setter。

    • @Persistent(mappedBy = "") 注释只能在非所有者方
    • 在所有者/子方面,所有者/父对象不应存在任何 getter/setter。

    工作代码:

    User.java

    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;
    
    @PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
    public class User {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;
    private String name;
    
    @Persistent(dependent = "true")
    private ContactInfo contactInfo;
    
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public ContactInfo getContactInfo() {
        return contactInfo;
    }
    
    public void setContactInfo(ContactInfo contactInfo) {
        this.contactInfo = contactInfo;
    }
    }
    

    ContactInfo.java

    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 com.google.appengine.api.datastore.Key;
    
    @PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
    public class ContactInfo {
        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Key id;
    
        @Persistent(mappedBy = "contactInfo")
        /*
         * Important: Do not create getter and setters for this object else
         * bidirectional mapping gives error
         */
        private User user;
    
        private String contactDetail;
    
        public Key getId() {
            return id;
        }
    
        public void setId(Key id) {
            this.id = id;
        }
    
        public String getContactDetail() {
            return contactDetail;
        }
    
        public void setContactDetail(String contactDetail) {
            this.contactDetail = contactDetail;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-03-23
      • 2014-11-15
      • 1970-01-01
      • 1970-01-01
      • 2021-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多