【问题标题】:ManyToMany Ebean mapping not working from ViewManyToMany Ebean 映射在 View 中不起作用
【发布时间】:2015-03-30 01:41:13
【问题描述】:

大家好,

我有 2 个模型(产品和 ProductCategory),它们以多对多关系相互关联。但是,当我尝试保存包含应该映射到 ProductCategory 的 html 选择字段的产品表单时,我得到一个

{"product_categories":["Invalid value"]}

我的模特是

Products.class
  public class Products extends Model{
    @ManyToOne
    public ProductCategories product_categories;
    ........Other methods and variables
  } 



ProductCategories.class
  public class ProductCategories extends Model
     @OneToMany(cascade = CascadeType.ALL, mappedBy = "product_categories")
     public List<Products> product = new ArrayList<>();
     ..........othee methods and variables
  }

景色是这样的

<select name="product_categories" class="form-control required">
     @for(prodCategory <- productCategories){
        <optionvalue="@prodCategory.getId()">@prodCategory.getCategory_name()</option>
} 
</select>

我实际上还更新了 2 个模型之间的 OneToMany 关系,并且收到了相同的错误消息。我也尝试了我在谷歌的一些帖子上看到的我应该在视图中放置一个 [] 的内容,即像

&lt;select name="product_categories[]" class="form-control required"&gt; 也是同样的问题。我可以使用request().body().asFormUrlEncoded() 确认视图正在将视图中的所有数据发送回控制器

请问有谁知道让映射工作的方法吗?

【问题讨论】:

  • 你试过this
  • 是的,我之前看过帖子。但正如我在上面的帖子中所说,我的控制器中没有超过if (productFormData.hasErrors()) {,而你的控制器至少为 Joads 保存到数据库中。但是,我查看了视图,发现您使用的格式与我正在使用的格式不同,您使用了field_name.id,所以我更改了我的格式,现在可以使用了。所以在我看来,我添加了 .id 而不是 product_categories。

标签: java ebean playframework-2.3


【解决方案1】:

感谢@singhakash 让我回到他自己的帖子中遇到类似问题。我注意到他的视图名称包括.id,例如,我使用product_categories 作为选择名称,他使用product_categories.id 在我将我的视图名称更改为使用他在his post 中概述的步骤之后。如果没有该更改,我将无法超越验证步骤。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 2013-04-26
    • 2014-07-19
    • 2011-09-07
    • 2012-01-14
    • 1970-01-01
    相关资源
    最近更新 更多