I try to store a Google User (com.google.appengine.api.users.User) inside of an own class in google datastore using objectify. I am using objectify (com.googlecode.objectify) version 5.0.3 and (com.sappenin.objectify) version 5.0.2. This is my class:
@Entity
public class UserTest {
@Id
public Long id;
@Index
public User user;
public UserTest() {}
public UserTest(User user) {
super();
this.user = user;
}
}
Now I try to store an object of this class using the following servlet class:
import static OfyService.ofy;
@Path("/userStore")
public class UserStoreTestService {
@POST
@Produces(MediaType.APPLICATION_JSON)
public User saveUser() {
Key<UserTest> key = ofy().save().entity(new UserTest(getUser())).now();
UserTest userTest = ofy().load().key(key).now();
return userTest.user;
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public User loadUser() {
UserTest userTest = ofy().load().type(UserTest.class)
.filter("user", getUser()).first().now();
return userTest.user;
}
private User getUser() {
return UserServiceFactory.getUserService().getCurrentUser();
}
}
My ofy service looks like that:
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyFactory;
import com.googlecode.objectify.ObjectifyService;
public class OfyService {
static {
factory().register(UserTest.class);
}
public static Objectify ofy() {
return ObjectifyService.ofy();
}
}
When I do a POST request i get this JSON code:
{"email":"test@example.com","authDomain":"gmail.com","userId":"18580476422013912411","federatedIdentity":null,"nickname":"test@example.com"}
When I do a GET request after the POST I get this JSON code:
{"email":"test@example.com","authDomain":"gmail.com","userId":"-1405876145","federatedIdentity":null,"nickname":"test@example.com"}
The first Id is 18580476422013912411 the second one is -1405876145.
What am I doing wrong?