Welcome to the new Gigaspaces XAP forum. To recover your account, please follow these instructions.

Ask Your Question
0

How to save reference to anther space class in some space class

Hi,

I came across to some problem. Here is my code:

Space class which should contain reference to another space class (SpaceClassB):

@SpaceClass
public class SpaceClassA {

    private String spaceId;

    private SpaceClassB spaceClassB;

    public SpaceClassA(SpaceClassB spaceClassB) {
        this.spaceClassB = spaceClassB;
    }

    public SpaceClassA() {
    }

    @SpaceId(autoGenerate = true)
    public String getSpaceId() {
        return spaceId;
    }

    public void setSpaceId(String spaceId) {
        this.spaceId = spaceId;
    }

    public SpaceClassB getSpaceClassB() {
        return spaceClassB;
    }

    public void setSpaceClassB(SpaceClassB spaceClassB) {
        this.spaceClassB = spaceClassB;
    }

    @Override
    public String toString() {
        return "[SpaceClassA]{ spaceid = " + spaceId + ", spaceClassB = " + spaceClassB + "}";
    }
}

...

@SpaceClass
public class SpaceClassB {

    private String spaceId;

    private String message;

    public SpaceClassB(String message) {
        this.message = message;
    }

    public SpaceClassB() {
    }

    @SpaceId(autoGenerate = true)
    public String getSpaceId() {
        return spaceId;
    }

    public void setSpaceId(String spaceId) {
        this.spaceId = spaceId;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return "[SpaceClassb]{ spaceid = " + spaceId + ", message = " + message + "}";

    }

...

private GigaSpace gigaSpace;

@Before
public void setUpGigaSpace() {
    UrlSpaceConfigurer urlSpaceConfigurer = new UrlSpaceConfigurer("/./ExampleSpace");
    gigaSpace = new GigaSpaceConfigurer(urlSpaceConfigurer).gigaSpace();
}

@test
public void shouldSaveToSpace() {
    //save spaceObjectB to space
    gigaSpace.write(new SpaceClassB("Not updated message"));
    //save spaceObjectA to space which should have reference to spaceObjectB
    SpaceClassB spaceClassBFromSpace = gigaSpace.read(new SpaceClassB());
    gigaSpace.write(new SpaceClassA(spaceClassBFromSpace));
    System.out.println("SpaceClassA object before updating SpaceClassB object :"
            + gigaSpace.read(new SpaceClassA()));
    //update spaceObjectB in space
    SpaceClassB read = gigaSpace.read(new SpaceClassB());
    read.setMessage("Updated message");
    gigaSpace.write(read);
    //reference in spaceObjectA to spaceObjectB not updated!
    System.out.println("SpaceClassA object after updating SpaceClassB object :"
            + gigaSpace.read(new SpaceClassA()));
}

...

   SpaceClassA object before updating SpaceClassB object :[SpaceClassA]{ spaceid = A0^1404378588284^2, spaceClassB = [SpaceClassb]{ spaceid = A0^1404378588284^1, message = Not updated message}}
    SpaceClassA object after updating SpaceClassB object :[SpaceClassA]{ spaceid = A0^1404378588284^2, spaceClassB = [SpaceClassb]{ spaceid = A0^1404378588284^1, message = Not updated message}}

So here is my question..How space classes save another space class in itself? It is solved by saving reference or save serialized object?

Thanks Kamil.

asked 2014-07-03 04:31:27 -0600

Kamil K gravatar image
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

SpaceClassB treated like any other nested object within space class. It is stored within SpaceClassA based on the storage type (serialization mode) used : Binary (serialized) , Compressed or Object (de-serialized).

Two SpaceClassA referring to the same SpaceClassB will result duplicated SpaceClassB within the space. You should avoid this by making SpaceClassB a space class by itself and save SpaceClassB ID within SpaceClassA.

See: http://docs.gigaspaces.com/sbp/modeli...

Shay

answered 2014-07-03 05:24:27 -0600

shay hassidim gravatar image
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-07-03 04:31:27 -0600

Seen: 560 times

Last updated: Jul 03 '14