播放框架复选框表单和JPA映射

时间:2015-05-28 18:12:06

标签: java hibernate jpa checkbox playframework

我遇到与ManyToMany关系有关的问题。问题发生在我提交FORM时,我的控制器不仅保存了JOIN表的映射,还为Type类创建了一个新的条目。

我的代码:

查看:

resetValues="true"

型号: 产品型号

commandButton

输入型号:

@main("Create Form"){


    <h1 class="page-header">New Products Form</h1> 
    <div class="row placeholders div-table">
        <div class="col-xs-12 col-sm-12 div-table-row">
            @helper.form(routes.Products.createProducts){
                <div class="form-group">
                @helper.inputText(form("name"), '_label->"Product Name", 'id->"productName", 'name->"productName", 'class ->"form-control", 'placeholder ->"Enter ProductName") 
                </div><!--End First Form Group-->

                <div class="form-group">

                @for(type <- types) {
                <div class="checkbox">
                    <label>
                        <input type="checkbox" name='@(form("types").name)' id="@type" value="@type">
                        @type
                    </label>
                </div>
                }
                </div>
                <button class="btn btn-default">Submit</button>
                } 

控制器:

@Entity
@Table(name = "Products")
public class Product{

    // Class Attributes
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "PRODUCT_ID")
    public Long id;
    public String name;

    @ManyToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER)

    public List<Type> types;

public Product save() {
        JPA.em().persist(this);
        return this;
    }

} 

1 个答案:

答案 0 :(得分:0)

您是否尝试使用jpa注释声明您的@ManyToMany关联,并明确表达您的关联:

@ManyToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
@JoinTable(name = "relation_product_type",
    joinColumns = {@JoinColumn(name = "product_id", referencedColumnName = "PRODUCT_ID")},
    inverseJoinColumns = {@JoinColumn(name = "type_id", referencedColumnName = "TYPE_ID")})

它将在此基础上为此关系创建一个表格。

你应该看看@ManyToMany注释中定义的级联属性(为了避免你要删除产品并删除产品及其类型的情况)http://www.oracle.com/technetwork/middleware/ias/toplink-jpa-annotations-096251.html

相关问题