1

Let's say I have

@Entity
public class Car implements Serializable{

    @Id private Long id = null;
    @Index private Driver driver = null;
    ...
}

and

@Entity
public class Driver implements Serializable{

    @Id private Long id = null;
    ...
}

How can I filter a Car entity by the id of his driver entity in Objectify? Something like ofy().load().type(Car.class).filter("driver.id", someId).first().now();

Thanks in advance.

1 Answer 1

1

At present, you can't. And even if you could, it would almost certainly be something awkward like filter("driver.__key__", Key.create(Driver.class, someId)). This is getting pretty far into unexplored territory. Unless you're super familiar with GAE and Objectify, you're best off dropping the @Id annotation and treating embedded entities just like regular embedded objects. There's no reason why id can't just be a regular (indexed) property.

4
  • But I cannot drop the @Id annotation because every entity has to have one field annotated with @Id
    – Mike W
    Aug 31, 2015 at 13:40
  • Embedded objects do not need to be @Entity. They can be regular POJOs. Actually embedding @Entity objects is possible, but it has some what more complicated meaning than what you likely want to do (it sets the key of the EmbeddedEntity). Unless you really know what you are doing, you probably want keyless embedded things. Sep 2, 2015 at 8:01
  • But I need them to be @Entity, I use them in other part of my code
    – Mike W
    Sep 8, 2015 at 12:59
  • In that case, duplicate the id field into a real @Indexed field. Populate it in an @OnSave method and don't provide any getters or setters so it's hidden from the rest of your code. Keys are special in the datastore and ids, being one component of a key, are not uniquely filterable. Sep 8, 2015 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.