Search code examples
google-app-enginepersistencesetplayframeworksiena

Play Gae persistence not working for Set<Long>


This is my User class

public class User extends Model {

@Id
public Long id;

public String nome;
public String email;
public String webId;    //ID of the user in the provider website
public String passwordHash;
public String service;

//Relazioni
private Set<Long> idEvents = new HashSet<Long>();
...
private Set<Long> idPhotos= new HashSet<Long>();


public User(String email, String name,String webId, String passwordHash, String service) throws Exception {
    ...
}

static Query<User> all() {
    return Model.all(User.class);
}

public static User findByEmail(String email){
    return all().filter("email", email).get();
}
}

When I create it and insert it to the database it seems to work fine. But then when I recall it from the DB using findByEmail(email). It loads a user with all the Sets (like idEvents) nulls.

I'm using play 1.1 with siena and gae modules.

Any idea on what could be wrong? I tried declaring the sets public and using a list instead of a set, but nothing worked.

Thanks


Solution

  • I'm the lead developer of Siena and the problem comes from your model. You can't declare your relation like that.

    First of all, do you want you idEvents and idPhotos to be store directly into your object using JSON serialization? If yes, you should use the @Embedded:

    @Embedded
    public List<Long> idEvents;
    

    In this case, the idEvents are retrieved automatically when you do:

    List<User> users = User.all()...fetch();
    

    If no, you should use the automatic queries which is the simple way to design a Many2One relation in Siena. Basically, you will create a relation between the User and the Event (I suppose you already have this class)

    @Filter("owner")
    public Query<Event> events; // this is called an "automatic-query"
    

    If you are not using the new version of Siena with play (v1.0.0 being test nowadays with Play), you would use the following code as there is no JOIN in GAE and you have to get the linked entities manually:

    User user = someUser();
    List<User> theEvents = user.events.fetch();
    

    It's globally explained there: http://www.sienaproject.com/documentation-getting-started.html

    Siena is deeply refactored those last days: it is being improved and also enhanced with lots of new features. Current version is version 1.0.0_b2.
    I hope we will deliver the final version 1.0.0 soon and write lots of docs to explain everything a bit better than now ;)

    If you have any question, don't hesitate to ask me!