GAE unowned JPA relationship
-
05-07-2019 - |
Question
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?
Solution
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow