gpt4 book ai didi

java - 如何将自定义数据附加到 ebean 实体?

转载 作者:搜寻专家 更新时间:2023-10-30 23:16:44 24 4
gpt4 key购买 nike

假设我有以下类(class)

@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 集合中存储所有订单的总和?

最佳答案

也许你应该看看 Formula annotation .

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

@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);
...
}
...
}

关于java - 如何将自定义数据附加到 ebean 实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12348416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com