2017-08-07 12 views
1

@angular/http method http.put()を使用してモデルを更新することに問題があります。角4はネストされたフィールドを更新します

問題は、ポジションを更新できないことです。私は正常に他のフィールドを更新することができ、POSTで作成するときに任意の位置を設定することができます。

public interface EmployeeProjection { 

    Long getId(); 

    String getName(); 

    String getEmail(); 

    String getPhone(); 

    Date getBirthDay(); 

    @Value("#{target.position.name}") 
    String getPosition(); 
} 

と位置クラス:

public class Position { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    Long id; 

    String name; 
} 

私の角度のバージョンは私のモデルは

public class Employee { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    Long id; 

    String name; 

    String email; 

    String phone; 

    Date birthDay; 

    @JsonBackReference 
    @ManyToOne(fetch = FetchType.EAGER) 
    @JoinColumn(name = "position_id") 
    Position position; 
} 

プロジェクションのように見えるJavaで "^ 4.3.3"

です

角形テンプレートEポジション:コンポーネントで

<md-select mdInput placeholder="Position" 
      [(ngModel)]="newEmployee.position"> 
    <md-option *ngFor="let position of positions | async" [value]="position.name">{{ position.name }} 
    </md-option> 
</md-select> 

私の更新方法:

update() { 
    let positionName = this.employee.position; 
    this.positionService.findByName(positionName).subscribe(position => { 
     this.employee.position = position._links.self.href; 
     this.employeeService.update(this.employee); 
     this.employee.position = positionName; 
     this.employeeService.localStorageService.set('employee', this.employee); 
    }); 
    } 

とサービスで:クロム要求で

update(employee: Employee) { 
    this.http.put(employee._links.self.href, employee) 
     .map((resp: Response) => resp.json()) 
     .subscribe(() => { 
     this.getAll(); 
     }); 
    return employee; 
    } 

{ 
    "name": "Nikolai Morgan", 
    "id": 1, 
    "position": "http://localhost:9080/api/positions/5", 
    "birthDay": "1986-07-01", 
    "email": "[email protected]", 
    "phone": "+380840713229", 
    "_links": { 
    "self": { 
     "href": "http://localhost:9080/api/employees/1" 
    }, 
    "employee": { 
     "href": "http://localhost:9080/api/employees/1{?projection}", 
     "templated": true 
    }, 
    "position": { 
     "href": "http://localhost:9080/api/employees/1/position" 
    } 
    } 
} 

しかし、応答してプレビューではありませんフィールドを含む位置:

{ 
    "id" : 1, 
    "name" : "Nikolai Morgan", 
    "email" : "[email protected]", 
    "phone" : "+380840713229", 
    "birthDay" : "1986-07-01", 
    "_links" : { 
    "self" : { 
     "href" : "http://localhost:9080/api/employees/1" 
    }, 
    "employee" : { 
     "href" : "http://localhost:9080/api/employees/1{?projection}", 
     "templated" : true 
    }, 
    "position" : { 
     "href" : "http://localhost:9080/api/employees/1/position" 
    } 
    } 
} 
+0

ので、ハムレットの質問は何ですか? '? ;) – Cepr0

+0

モデルのネストされたフィールドの値を更新する方法は? – MolecularMan

+0

私の答えを参照してください... – Cepr0

答えて

1

別のものへの参照を持つエンティティを更新するには、Spring Data RESTでこのエンティティへのリンクを使用する必要があります。たとえば:

POST http://localhost:9080/api/positions

{ 
    "name": "position1" 
} 

そして、このような応答を取得:

{ 
    "name": "position1", 
    "_links" : { 
     "self" : { 
      "href" : "http://localhost:9080/api/positions/1" 
     }, 
     "position" : { 
      "href" : "http://localhost:9080/api/positions/1" 
     } 
    } 
} 

はその後従業員を追加

@Entity 
class Employee { 
    //... 

    String name; 

    @ManyToOne 
    Position position; 

    //... 
} 

@Entity 
class Position { 
    //... 

    String name; 

    //... 
} 

interface EmployeeRepo extends JpaRepository<Employee, Long> {} 
interface PositionRepo extends JpaRepository<Position, Long> {} 

まず、位置を追加します

PUT http://localhost:9080/api/employees/1

{ 
    "name": "employee1", 
    "position": "http://localhost:9080/api/positions/2" 
} 
:私たちは、その後、従業員をPUT、我々は新しいものを作成する位置を更新する必要があるのであれば

{ 
    "name" : "employee1", 
    "_links" : { 
    "self" : { 
     "href" : "http://localhost:9080/api/employees/1" 
    }, 
    "employee" : { 
     "href" : "http://localhost:9080/api/employees/1", 
    }, 
    "position" : { 
     "href" : "http://localhost:9080/api/employees/1/position" 
    } 
    } 
} 

:10 POST http://localhost:9080/api/employees

{ 
    "name": "employee1", 
    "employee": "http://localhost:9080/api/positions/1" 
} 

その後の応答を取得

またはそれを貼り付けてください:

PATCH http://localhost:9080/api/employees/1

{ 
    "position": "http://localhost:9080/api/positions/2" 
} 
+0

ソリューションのおかげで、私はパッチとバリアントを選んだ。また、既存のポジションと従業員を使用してPUTを使用して更新する方法を明確にすると、私は感謝します – MolecularMan

+0

どういう意味ですか?私は答えの中でPUTを使った更新例を示しました... – Cepr0

+0

私のコードでは、私は位置のリンクも設定していますが、それは起動しませんでした。私は自分のミスがどこにあるのかを理解したい。 – MolecularMan

関連する問題