title: Spring Rest with Angular 2
body: <p>I am having problems with inserting new rows into database with my Angular 2 application.</p>
  
  <p>The problem occures when i try to insert a new entry in database that has a foreign key to another table in the database. My two models defined in Spring are:</p>
  
  <pre><code>@Entity
  public class A{
  
      @Id
      @NotNull
      @GeneratedValue(strategy = GenerationType.AUTO)
      private int id;
  
      @OneToMany(mappedBy = "A")
      private List&lt;B&gt; b = new ArrayList&lt;B&gt;();
      ...
  }
  
  @Entity
  public class B{
  
      @Id
      @NotNull
      @GeneratedValue(strategy = GenerationType.AUTO)
      private long id;
  
      @NotNull
      @ManyToOne
      @JoinColumn(name="aId")
      private A a;
      ...
  }
  </code></pre>
  
  <p>Those two models in backend have their counterparts in the frontend. I added the _links part because Spring Rest api gives links instead of foreign keys:</p>
  
  <pre><code>export class A{
    id: number;
  
    b: B[];
  
    _links:{...}
    ...
  }
  
  export class B{
    id: number;
  
    a: A;
  
    _links:{...}
    ...
  }
  </code></pre>
  
  <p>I created these models based on what information i get from api. For example, a get request on localhost:8080/api/b/1 gives:</p>
  
  <pre><code>{
    "id" : 1,
    "_links" : {
      "self" : {
        "href" : "http://localhost:4200/api/b/1"
      },
      "b" : {
        "href" : "http://localhost:4200/api/b/1"
      },
      "a" : {
        "href" : "http://localhost:4200/api/b/1/a"
      }
    }
  }
  </code></pre>
  
  <p>I can easily insert new rows into table A (since it doesn't contain foreign keys) with my angular 2 service method shown bellow:</p>
  
  <pre><code>  createA(a: A): Observable&lt;A[]&gt;{
      return this.http.post(this.AUrl, a)
                      .map((res:Response) =&gt; res.json())
                      .catch((error:any) =&gt; Observable.throw(error.json().error || 'Server Error'));
    }
  </code></pre>
  
  <p>Similarly, service method for creating a new model B looks like:</p>
  
  <pre><code>  createB(b: B): Observable&lt;B[]&gt;{
      return this.http.post(this.BUrl, b)
                      .map((res:Response) =&gt; res.json())
                      .catch((error:any) =&gt; Observable.throw(error.json().error || 'Server Error'));
    }
  </code></pre>
  
  <p>My repositories in Spring are defined as:</p>
  
  <pre><code>@RepositoryRestResource(collectionResourceRel="a", path="a", itemResourceRel="a")
  public interface ARepository extends JpaRepository&lt;A, Integer&gt;{
  }
  
  @RepositoryRestResource(collectionResourceRel="b", path="b", itemResourceRel="b"))
  public interface BRepository extends JpaRepository&lt;B, Long&gt;{
  }
  </code></pre>
  
  <p>My repository configuration in Spring is:</p>
  
  <pre><code>  @Configuration
  public class MyRestConfiguration extends RepositoryRestMvcConfiguration{
  
      @Override
      protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config){
          config.setBasePath("/api");
          config.exposeIdsFor(A.class);
          config.exposeIdsFor(B.class);
      }
  }
  </code></pre>
  
  <p>When i try to insert a new row into the table B i get the following error in spring:</p>
  
  <pre><code>javax.validation.ConstraintViolationException: Validation failed for classes [......model.B] during update time for groups [javax.validation.groups.Default, ]
  List of constraint violations:[
      ConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath=a, rootBeanClass=class ........model.B, messageTemplate='{javax.validation.constraints.NotNull.message}'}
  ]
  </code></pre>
  
  <p>I would like to know what exactly is the request payload for http post request supposed to look like, in order for the application to insert a new row of B into the database. Any help is greatly appreciated.</p>
  
  <p>Edit:</p>
  
  <p>I have made a spring rest controler with this mapping:</p>
  
  <pre><code>@PostMapping("/api/b")
  @ResponseBody
  public String createServis(@RequestBody B b){
  
  }
  </code></pre>
  
  <p>If I make a http post request with this payload (using curl or by some other means, but thats not the point):</p>
  
  <pre><code>{
      id:1,
      aId:1
  }
  </code></pre>
  
  <p>or this one:</p>
  
  <pre><code>{
      id:1,
      a:{id:1}
  }
  </code></pre>
  
  <p>or even this one:</p>
  
  <pre><code>{
      id:1,
      aId:1,
      a:{id:1}
  }
  </code></pre>
  
  <p>The property a of the object b remains null / is not properly initialized.</p>