2016-09-18 25 views
1

1対1の関係を持つエンティティの保存に関する問題を解決しようとしています。POSTネストされたオブジェクトRest Spring MVC AngularJS

フィールドアパートメントを持つテナントオブジェクトがあります。

のparams
Params

応答: Response

場合、私は新しいテナントオブジェクトを保存しようとするのparamsは、私にはOKと思われるが、毎回私は400エラーを取得します入力フォームからアパートを削除して、テナントが問題なく保存されるようにします。

エンティティを関連性とともに保存するにはどうすればよいですか? JSPページ

@RequestMapping(method = RequestMethod.POST) 
    public ResponseEntity<?> create(@Valid @RequestBody Tenant tenant, BindingResult result) { 
     if (result.hasErrors()) { 
      ApiResponse message = new ApiResponse(); 
      message.setErrors(result.getFieldErrors()); 
      return new ResponseEntity<ApiResponse>(message, HttpStatus.UNPROCESSABLE_ENTITY); 
     } 
     try { 
      service.saveTenant(tenant); 
     } catch (ConstraintViolationException e) { 
      result.rejectValue("apartmentNumber", "error.apartment", DUPLICATE_VALUE); 

      Map<String, String> test = new HashMap<>(); 
      for (FieldError fieldError : result.getFieldErrors()) { 
       test.put(fieldError.getField(), fieldError.getDefaultMessage()); 
      } 
      return new ResponseEntity<Map<String, String>>(test, HttpStatus.CONFLICT); 
     } 
     return new ResponseEntity<Tenant>(tenant, HttpStatus.CREATED); 
    } 

RestController - 問題の入力

<div class="form-group"> 
    <label for="inputEmail3" class="col-sm-3 control-label">Mieszkanie</label> 
    <div class="col-sm-9"> 
     <select name='apartment' ng-model="ctrl.tenant.apartment" ng-required='true' class="form-control"> 
      <option ng-repeat="item in ctrl.apartments" value="{{item}}" 
      ng-selected="ctrl.tenant.apartment.description == item.description"> 
      {{item.description}}</option> 
     </select> 
     <p class="help-block"> 
      <span class='error' ng-show="myForm.tenant_apartment.$invalid"> 
       Pole wymagane</span> <span class='error'>{{errors.tenant_apartment}}</span> 
     </p> 
    </div> 
</div> 

答えて

0

は、あなたのテナントクラスがセットタイプとしてアパートのメンバ変数を持っています。 それだけで `@OneToOne 民間のアパートのアパートですので、私は1対1の関係

 
public class Tenant implements Serializable { 

    private Set<Appartment> appartment; 
    ..... 
    ..... 
} 
+0

その意味;' – Maciej

関連する問題