0
votes

I'm beginner in Android and I'm trying to understand how GAE works with Objectify.

So I've created two classes, one 'User' and another one 'Journey'. Each Journey belongs to a User.

User Class

@Entity
public class User {
    @Id
    private Long id;
    @Index private String mac;
    private String name;
    private String firstName;
    private Long age;
    private String email;
    private String password;
// Getters and setters
}

Journey Class

@Entity
public class Journey {
    @Id
    private Long id;
    Key<User> driver;
    private Event event;
    private Long nbPlaces;
    private String departureTime;
    private String destination;
}
  1. I've writed the following method on the User class, is this correct ?

    @Transient Key getKey() { return Key.create(User.class, id); }

  2. How can I set the Key of the User in my Journey Object ? (I think I can't use a simple setter.

Thanks !

1

1 Answers

0
votes

Previously declared:

import static com.googlecode.objectify.ObjectifyService.ofy;

To get the object:

public User get(Long id) {
     return ofy().load().key(Key.create(User.class, id)).now();
}

And to set the Key from User into Journey class, you need to pass into the constructor when the object is created, or get the object Journey set the parameters and save it. But you need to get the Key previously:

public Long getKey(User user) {
     Key<User> generatedKey = ofy().save().entity(user).now();
     return generatedKey.getId();
}

After this, you can attach a list of Users into Journeyclass.