0

I have some troubles to update Many-To-Many Relationship with checkboxes in Play Framework 2.1.0

Account Model:

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "account_security_role", joinColumns = { @JoinColumn(name = "account_id", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "security_role_id", referencedColumnName = "id") })
public List<SecurityRole> roles;

I want to update account roles in account edit view

Edit Account View:

@for(role <- SecurityRole.find.all()) {
   <div class="control-group">
      <label class="control-label"> @role.description </label>
      <div class="controls">
         <div class="text-toggle-button">
            <input type="checkbox" name="roles[@role.id].id" value="@role.id" class="toggle"
            @if(loggedAccount.hasHole(role.id)) {
               CHECKED
            } />
         </div>
      </div>
   </div>
}

So, when I call Update POST method I get this error:

InvalidPropertyException: Invalid property roles[3] of bean class [models.Account]: Illegal attempt to get property roles threw exception; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property roles of bean class [models.Account]: Could not instantiate property type [be.objectify.deadbolt.core.models.Role] to auto-grow nested property path: java.lang.InstantiationException: be.objectify.deadbolt.core.models.Role

Is there some example to update a model using checkbox and Many-To-Many relationship?

Thanks a lot. Lew

4

1 回答 1

0

谢谢阿尔班,我发现了错误。

模型中的属性名称有一个约定,因此当我将名称从角色更改为securityRoles时,它工作正常并找到了正确的模型类进行实例化。

账户模式:

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "account_security_role", joinColumns = { @JoinColumn(name = "account_id", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "security_role_id", referencedColumnName = "id") })
public List<SecurityRole> securityRoles;

编辑帐户视图:

@for(role <- SecurityRole.find.all()) {
   <div class="control-group">
      <label class="control-label"> @role.description </label>
      <div class="controls">
         <div class="text-toggle-button">
            <input type="checkbox" name="securityRoles[@role.id].id" value="@role.id" class="toggle"
            @if(loggedAccount.hasHole(role.id)) {
               CHECKED
            } />
         </div>
      </div>
   </div>
}
于 2013-04-10T16:47:01.473 回答