JDO 和 AppEngine 的子属性

发布于 2024-09-10 06:37:01 字数 793 浏览 3 评论 0原文

我有以下代码:

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

@Persistent
private B b;

@Persistent
private int id;

    // ...

@PersistenceCapable

(identityType = IdentityType.APPLICATION, detachable="true") 公共B类{ @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) @PrimaryKey 私钥密钥;

@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的引用为空

有人能告诉我我做错了什么吗?

谢谢!

I have the following code:

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

@Persistent
private B b;

@Persistent
private int id;

    // ...

}

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

@Persistent
private int id;

    // ...

}

Now what I need to be able to do, is retrieve an instance of B, and refer to it from an instance of A like this:

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

When I pass a to the makePersistent() method of the PersistenceManager, two things that I don't need happen:

1) a new instance of B is created
2) the reference A makes to b is null

Could someone tell me what I am doing wrong?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海拔太高太耀眼 2024-09-17 06:37:01

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

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

import java.io.Serializable; 

public class DownloadableFile implements Serializable { 
    private byte[] content; 
    private String filename; 
    private String mimeType; 

    // ... accessors ... 

}要将 Serialized 类的实例存储为属性中的 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;  

A field value can contain an instance of a Serializable class, storing the serialized value of the instance in a single property value of the type Blob. To tell JDO to serialize the value, the field uses the annotation @Persistent(serialized=true). Blob values are not indexed and cannot be used in query filters or sort orders.

Here is an example of a simple Serializable class that represents a file, including the file contents, a filename and a MIME type. This is not a JDO data class, so there are no persistence annotations.

import java.io.Serializable; 

public class DownloadableFile implements Serializable { 
    private byte[] content; 
    private String filename; 
    private String mimeType; 

    // ... accessors ... 

}To store an instance of a Serializable class as a Blob value in a property, declare a field whose type is the class, and use the @Persistent(serialized = "true") annotation:

import javax.jdo.annotations.Persistent; 
import DownloadableFile; 

// ... 
    @Persistent(serialized = "true") 
    private DownloadableFile file;

I your case you can use

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

Then declare it in your data class

@Persistent(serialized = "true")     
private B b;  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文