0
votes

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?

1
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. What happens if you change your query to List<Payment> payments = ofy().load().type(Payment.class) .filter("parent = ", Ref.create(group)).list(); ?tx802
oh well thank you that solved my problem. i tried something similar to that but it did not work. Tkans!robinbuehler
Great to hear. I've added my comment as an answer in case you want to accept it.tx802

1 Answers

2
votes

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.