Play2映射模板中的对象

时间:2013-09-16 11:11:55

标签: java scala playframework playframework-2.0

我有一个play2项目,我想创建一个新产品。产品包含一个存储在几个表中的KeyInfo。

这是我对新产品的临时性

@(newProductForm:Form[models.Product], keyInfoList: List[Keyinfo] )
@adminMain(""){
@helper.form(action = routes.Admin.insertNewProduct()) {

    @helper.inputText(
        newProductForm("name"),
        'label -> "name",
        'type -> "name"
    )

    @helper.inputText(
        newProductForm("price"),
        'label -> "price",
        'type -> "price"
    )

    @helper.inputText(
        newProductForm("shortDescription"),
        'label -> "shortDescription",
        'type -> "shortDescription"
    )

    @helper.inputText(
        newProductForm("description"),
        'label -> "description",
        'type -> "description"
    )


    @helper.select(
    newProductForm("keyinfo"),
    helper.options(
        for(info <- keyInfoList) yield info.keyinformation

        )
    )

    <button type="submit">Add</button>
 }
}

keyinfo的select帮助程序正在从表中正确获取所有Fieldnames。问题是,KeyInformation的id不存储在ProductTable中。

以下是保存产品的Controller功能

    public static Result insertNewProduct() {
    Form<Product> productForm = form(Product.class).bindFromRequest();
    return ok(showNewProduct.render(Product.create(productForm.get())));

    }

带有创建功能的产品型号

@Entity
public class Product extends Model {

@Id
//@Constraints.Required
//@Formats.NonEmpty
@Column(name="id")
public Integer id;

@Constraints.Required
public String name;

@Constraints.Required
public Float price;

@Constraints.Required
@Column(name="short_Description")
public String shortDescription;

@Constraints.Required
public String description;

@ManyToOne
@Constraints.Required
public Keyinfo keyinfo;



public static Product create(Product product){
    product.save();
    return product;
}

我希望有人可以帮助我

2 个答案:

答案 0 :(得分:0)

在没有看到整个项目的情况下不确定根本原因,但为了帮助您找到问题,您可以在尝试保存时转储调试消息以检查值:

public static Product create(Product product){
    play.Logger.info("Saving ... product name: " + product.name);
    play.Logger.info("Saving ... product key info: " + product.keyinfo.toString());
    ... 
    product.save();
    return product;
}

答案 1 :(得分:0)

如果您使用的是ebean,您可能需要先显式保存keyInfo,然后设置product.keyInfo并保存产品。