(Beginner) How could I retrive joined entities with Objectify ?

34 views
Skip to first unread message

Sébastien Vermeille

unread,
May 15, 2013, 12:34:43 PM5/15/13
to objectify...@googlegroups.com
Hi devs,

I'm beginning with Objectify I'm used of Relational Database so I encounter some pain with gae storage.

I made an entity called "Project" :

@Id
Long id

@index
String project

@Index
@Load
List<Ref<User>> developpers = new ArrayList<Ref<User>>();

Then I would like to do a method in my dao to retrive all project where a specified user is affected to :

public List<Project> getByUsername(String username) {
User user = DAOFactory.getUserDAO().getByUsername(username);
if(user != null) {
                        // This query is wrong I know. But I dont see how I can do ... in sql I would say "join user_project ... where user_project.id_user = ..." but I can't ^^
List<Project> founds = ofy().load().type(Project.class).filter(user, " in developpers").list();
return founds;
}
return null;
}

I didn't find something talking about it on the documentation does someone have any tips about it ? Thank you in advance :)

Sebastien Vermeille

Alejandro Gonzalez

unread,
May 16, 2013, 7:12:44 AM5/16/13
to objectify...@googlegroups.com
As far as i know you cannot filter in Ref<?> properties. Instead i would do something like:

@Entity
public class Developper {
  @Id Long id;
  List<Long> projects;
}

@Entity
public class Project {
  @Id Long id;
  String name;
}

public List<Project> getDevelopperProjects( Long developperId ){
  
  Developper dev = ofy().load().type(Developper.class).id( developperId ).get();
  return new ArrayList<Project>( ofy().load().keys( dev.projects ).values() );

}

public void addDevelopperToProject( Long projectId, Long developperId ){
   Developper dev = ofy().load().type(Developper.class).id( developperId ).get();
   dev.projects.add( projectId );
   ofy().save().entity( dev ).now();

Jeff Schnitzer

unread,
May 16, 2013, 11:35:16 AM5/16/13
to objectify...@googlegroups.com
This is not quite correct.  Ref<?> properties are simply keys in the datastore.  So a List<Ref<?>> is a List<Key>. You can't filter by properties on the referenced thing (that would be a join), but you can certainly filter by the key value itself.

The standard form for querying on a list property will work:

List<Project> founds = ofy().load().type(Project.class).filter(user, "developpers").list();

The 'user' value can be anything that represents a key - a Ref<?>, a Key<?>, a native datastore Key, or even a POJO entity of the appropriate type (as long as its id is set properly).

Jeff



--
You received this message because you are subscribed to the Google Groups "objectify-appengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objectify-appen...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages