views:

477

answers:

1

I have an unowned relationship in my Domain model

@Entity
public class A {
 @Id
 private String id;
 private Key firstB;
 private Key secondB;

 // getters & setters
}

@Entity
public class B {
 @Id
 private Key id;
 private String name;
 // getter & setter
}

KeyFactory.createKey(B.class.getSimpleName(), name) is the way I generate the Key for class B

I save B independently from A and assign it to an instance of A some time. The problem is that after saving A both fields firstB and firstA are null.

Any idea of what I'm doing wrong?

+1  A: 

Key objects are not persisted by default so require explicit annotation which is why you are seeing null values.

Try annotating firstB and secondB as @Enumerated (this should really be @Basic but there is a bug which prevents this from working):

@Entity
public class A {
    @Id
    private String id;

    @Enumerated
    private Key firstB; 

    @Enumerated
    private Key secondB;
}

Update: The latest SDK and DataNucleus JARs now correctly allow the use of @Basic.

Matthew Murdoch
This was not in the official docs, so thanks for the hint!
david
As it turns out with the latest SDK and the latest datanucleus jars, @Basic works as well.
david
Thanks for the update, I'll add it to the answer.
Matthew Murdoch