简体   繁体   English

如何在Play框架中从同一页面绑定多个对象

[英]How to bind multiple objects from the same page in Play Framework

For an object Customer, I have multiple orders. 对于对象客户,我有多个订单。 So, In one page, I have a customer and multiple order objects, 因此,在一页中,我有一个客户和多个订单对象,

 <select name="customer.order[0].orderType" id="orderType" >
    #{list orderTypes}
        <option value="${_.id}">${_.name}</option>
    #{/list}
</select>

<select name="customer.order[1].orderType" id="orderType" >
    #{list orderTypes}
        <option value="${_.id}">${_.name}</option>
    #{/list}
</select>

Model Customer: 样板客户:

public class Customer{

@OneToMany(mappedBy = "customer")
public List<Order> orders;

}

Model Order: 型号订单:

     public class Order{

    @ManyToOne
    public TypeOrder orderType;

}

Controller : 控制器:

 public static void saveCustomerOrder(Customer customer) {
    customer.save();

    System.out.println(customer.orderType.name + " " + customer.order.size() + " " + customer.order.get(0).orderType);
}

So I get the the order size as 2 ; 所以我得到的订单大小为2; but i dont get the data for the orderType. 但我没有得到orderType的数据。 Can anyone help me with this? 谁能帮我这个? (It doesn't persist in the database, but customer object is saved) (它不会持久存储在数据库中,但是已保存了客户对象)

When I do customer.save(); 当我做customer.save(); I would like customer to be saved and order to be saved; 我希望保存客户并保存订单; (this is just a simple sample program) (这只是一个简单的示例程序)

--EDIT--- - 编辑 - -

It works if I save the orders separately by looping through them. 如果我通过遍历订单分别保存订单,则可以使用。

for(Order o : customer.orders) 
  o.save(); 

I just want to know if it is possible to save the child model when i save the parent model. 我只想知道在保存父模型时是否可以保存子模型。 Am I missing some annotation? 我是否缺少一些注释?

The way Play 1.x works, as per documentation , you have to explicitly save every object you want to persist. 根据文档说明 ,Play 1.x的工作方式必须明确保存要保留的每个对象。 So yes, you have to iterate over all the objects. 所以是的,您必须遍历所有对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM