【问题标题】:Child properties with JDO and AppEngineJDO 和 AppEngine 的子属性
【发布时间】:2011-03-16 17:07:12
【问题描述】:

我有以下代码:

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true") 公共类 A { @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) @首要的关键 私钥密钥;

@Persistent
private B b;

@Persistent
private int id;

    // ...

}

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true") 公共类 B { @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) @首要的关键 私钥密钥;

@Persistent
private int id;

    // ...

}

现在我需要做的是检索 B 的实例,并从 A 的实例中引用它,如下所示:

B b = DAL.getBById(1); A a = 新 A(); a.setB(b);

当我将 a 传递给 PersistenceManager 的 makePersistent() 方法时,我不需要发生两件事:

1) 创建 B 的一个新实例 2) A 对 b 的引用为空

谁能告诉我我做错了什么?

谢谢!

【问题讨论】:

    标签: google-app-engine jdo


    【解决方案1】:

    字段值可以包含 Serializable 类的实例,将实例的序列化值存储在 Blob 类型的单个属性值中。为了告诉 JDO 序列化该值,该字段使用注释 @Persistent(serialized=true)。 Blob 值未编入索引,不能用于查询过滤器或排序顺序。

    这是一个简单的 Serializable 类的示例,它表示一个文件,包括文件内容、文件名和 MIME 类型。这不是 JDO 数据类,因此没有持久性注释。

    import java.io.Serializable; 
    
    public class DownloadableFile implements Serializable { 
        private byte[] content; 
        private String filename; 
        private String mimeType; 
    
        // ... accessors ... 
    

    }要将 Serializable 类的实例存储为属性中的 Blob 值,请声明类型为该类的字段,并使用 @Persistent(serialized = "true") 注释:

    import javax.jdo.annotations.Persistent; 
    import DownloadableFile; 
    
    // ... 
        @Persistent(serialized = "true") 
        private DownloadableFile file;
    

    我的情况你可以使用

    import java.io.Serializable;      
    public class B implements Serializable { 
        private int xx; 
        ....
        ..........
     }
    

    然后在你的数据类中声明它

    @Persistent(serialized = "true")     
    private B b;  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多