1

假设我有以下课程

@Entity
public class Customer extends Model {

    @Id
    public int id;
    public String email;
    @ManyToOne
    public List<Order> orders;
    public HashMap<String, Object> additionalData;

    public static Finder<String, Customer> find = new Finder<String, Customer>(String.class, Customer.class);

    public static List<Customer> getCustomersWithOpenOrders(){
        return find
                .fetch("orders")
                // with "order.state = 'open'" count > 0
                // add total sum of all orders to 'additionalData' collection
                .findList();
    }
}

如何存储additionalData集合中所有订单的总和?

4

1 回答 1

2

也许你应该看看公式注释

但是为了使用它,你必须创建一个新属性来存储这个值,然后你可以把这个值放在你的地图中:

@Entity
public class Customer extends Model {

    @Id
    public Integer id;
    public String email;
    @ManyToOne
    public List<Order> orders;

    @Transient
    @Formula(...) // write the query to compute the sum
    public Integer totalOrders;

    public HashMap<String, Object> additionalData;

    public static Finder<Integer, Customer> find = new Finder<Integer, Customer>(Integer.class, Customer.class);

    public static List<Customer> getCustomersWithOpenOrders(){
        ...// call the finder

        additionalData.put("sum", totalOrders);
        ...
    }
...
}
于 2012-09-10T10:14:27.303 回答