robinbuehler robinbuehler - 3 months ago 30
Java Question

Objectify 5 - filter embedded entity field doesn't work

I try to load a list of objects from my own class called 'Payment'. Now I do not want to load all payments but only those who have a specific 'Group' as parent (Group is also an own class).

This is the Payment class:

@Entity
public class Payment {
@Id private Long id;
@Index @Load private Ref<Group> parent;
// other fields, constructors, getters and setters
}


And this is my Group class:

@Entity
public class Group {
@Index @Id private Long id;
// other fields, constructors, getters and setters
}


The following is the method in which I try to load this list of payments:

public static List<Payment> getPaymentsByGroup(Group group) {
List<Payment> payments = ofy().load().type(Payment.class)
.filter("parent.id", group.getId()).list();
return payments;
}


But the result is always an empty list. But if I try to do something like this:

public static List<Payment> getPaymentsByGroup(Group group) {
List<Payment> temp = new ArrayList<>();
List<Payment> payments = ofy().load().type(Payment.class).list();
for (Payment payment : payments) {
if(payment.getParent().getId().equals(group.getId())){
temp.add(payment);
}
}
return temp;
}


I'll get my payments. I use Objectify 5. Because of the performance, the Payment class should be an own Entity and not be stored as a normal embedded object in the Group class. Otherwise the whole group object would have to be stored with all payments by adding one single payment.

What am I doing wrong? How can I filter a field in an embedded entity?

Answer

I think you might be confusing terminology here as I don't see a @Parent (entity group) or an embedded entity. I do see a Ref<?>, which is essentially a Key.

If you change your query to

List<Payment> payments = ofy().load().type(Payment.class).filter("parent = ", Ref.create(group)).list();

it should work.