views:

12

answers:

1

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!

A: 

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;  
Manjoor