views:

61

answers:

0

Hi,

I encounter problems while trying to implement GWT JDO features. Let's say there is a Data object that contains an Item object. In the datastore this would create a one-to-one relationship.

The problem is that when I try to get the object from the datastore, the 'item' field is always null. Of course I put the object to the datastore that contains the initalized Item object (end of the listing). When viewing the datastore, both Data and Item entities do exist.

Do I manage the one-to-one relationship wrong? What else is needed? The same situation is when I try to create one-to-many relatioship (an array of Item's)

Thanks for any hints.


Data.java:

package com.rafalrybacki.jdotest.client.model;

import java.io.Serializable;
import java.util.ArrayList;

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)
public class Data implements Serializable {

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;

@Persistent
private String symbol;

@Persistent
public Item item;

public Data(){}

// ...
}

Item.java:

package com.rafalrybacki.jdotest.client.model;

import java.io.Serializable;

import javax.jdo.annotations.Extension;
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)
public class Item implements Serializable {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
@Extension(vendorName="datanucleus", key="gae.encoded-pk", value="true")
private String encodedKey;

@Persistent
public String title;

public Item(){}

public Item(String title){
    this.title = title;
}

// ...
}

server-side implementation:

public void save(Data data) {
    PersistenceManager pm = PersistenceManagerFactoryGetter.get().getPersistenceManager();
    try {
        pm.makePersistent(data);
    } finally {
        pm.close();
    }
    return;
}

public Item load() {
    PersistenceManager pm = PersistenceManagerFactoryGetter.get().getPersistenceManager();
    List<Data> datas = new ArrayList<Data>();
    Data data0 = null;
    try {
        Query q = pm.newQuery(Data.class);
        datas = (List<Data>) q.execute();
        if (datas.size() > 0)
            data0 = pm.detachCopy(datas.get(0)); // get first item only

    } finally {
        pm.close();
    }
    return data0.item;  // always null !!!!!
}

On client-side I'm operating on such created data object (with item field that is not null)

Data d = new Data("data1");
d.item = new Item("item2");

service.save(d, ...); //rpc service call

// ...

service.load(...);