title: Angular + Spring boot REST, cannot post an entity with foreign key
body: <p>I created a simple crud with Angular + Spring boot. I couldn't find a solution to solve my problem. I cannot post an entity with foreign key. I have two entities Product and Category.</p>
  <p><strong>Category.java:</strong></p>
  <pre><code>@Entity
  @Table(name = &quot;category&quot;)
  public class ProductCategory {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = &quot;id&quot;)
      private Long id;
  
      @Column(name = &quot;category_name&quot;)
      private String categoryName;
  
      @JsonManagedReference
      @OneToMany(cascade = CascadeType.ALL, mappedBy = &quot;category&quot;)
      private Set&lt;Product&gt; products;
  
      public ProductCategory() {
      }
  
      public ProductCategory (Long id) {
          super();
          this.id = id;
      }
  
  
      public Set&lt;Product&gt; getProducts() {
          return products;
      }
  
      public void setProducts(Set&lt;Product&gt; products) {
          this.products = products;
      }
  
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public String getCategoryName() {
          return categoryName;
      }
  
      public void setCategoryName(String categoryName) {
          this.categoryName = categoryName;
      }
  }
  </code></pre>
  <p><strong>Product.java:</strong></p>
  <pre><code>@Entity
  @Table(name = &quot;product&quot;)
  public class Product {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = &quot;id&quot;)
      private Long id;
  
      @Column(name = &quot;brand&quot;)
      private String brand;
  
      @Column(name = &quot;name&quot;)
      private String name;
  
      @JsonBackReference
      @ManyToOne
      @JoinColumn(name = &quot;category_id&quot;, nullable = false)
      private ProductCategory category;
  
  
      public Product(Long id, String brand, String name, ProductCategory category) {
          this.id = id;
          this.brand = brand;
          this.name = name;
          this.category = category;
      }
  
      public Product(){}
  
      public ProductCategory getCategory() {
          return category;
      }
  
      public void setCategory(ProductCategory category) {
          this.category = category;
      }
  
      public Long getCategoryId() {
          return category.getId();
      }
  
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public String getBrand() {
          return brand;
      }
  
      public void setBrand(String brand) {
          this.brand = brand;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  }
  </code></pre>
  <p>When i save an entity via postman it works perfectly:</p>
  <pre><code>{
              &quot;brand&quot;: &quot;hp&quot;,
              &quot;name&quot;: &quot;envy&quot;,
              &quot;category&quot;: {
                  &quot;id&quot;: 2
              }
          }
  </code></pre>
  <p>But when i post it in Angular i get an error in Spring/ Here is the stacktrace:</p>
  <pre><code> org.hibernate.PropertyValueException: not-null property references a null or transient value : com.codejava.shoptest.entity.Product.category
      at org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:111) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
      at org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:55) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
      at org.hibernate.action.internal.AbstractEntityInsertAction.nullifyTransientReferencesIfNotAlready(AbstractEntityInsertAction.java:116) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
      at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:72) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
      at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:645) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
      at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:282) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
      at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:263) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
      at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:317) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
      at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:330) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
      at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:287) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
      at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:193) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
      at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:123) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
      at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:185) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
      at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:128) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
      at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:55) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
      at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:102) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
      at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:721) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
      at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:707) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
      at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
      at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:366) ~[spring-orm-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at com.sun.proxy.$Proxy93.persist(Unknown Source) ~[na:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
      at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
      at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:314) ~[spring-orm-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at com.sun.proxy.$Proxy93.persist(Unknown Source) ~[na:na]
      at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:554) ~[spring-data-jpa-2.3.3.RELEASE.jar:2.3.3.RELEASE]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
      at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
      at org.springframework.data.repository.core.support.ImplementationInvocationMetadata.invoke(ImplementationInvocationMetadata.java:72) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
      at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:382) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
      at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:205) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
      at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:549) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:155) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
      at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:130) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80) ~[spring-data-commons-2.3.3.RELEASE.jar:2.3.3.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367) ~[spring-tx-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118) ~[spring-tx-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:178) ~[spring-data-jpa-2.3.3.RELEASE.jar:2.3.3.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at com.sun.proxy.$Proxy98.save(Unknown Source) ~[na:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
      at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
      at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:205) ~[spring-aop-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at com.sun.proxy.$Proxy64.save(Unknown Source) ~[na:na]
      at com.codejava.shoptest.controller.ProductController.createProduct(ProductController.java:33) ~[classes/:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
      at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
      at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:652) ~[tomcat-embed-core-9.0.37.jar:4.0.FR]
      at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.37.jar:4.0.FR]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.37.jar:9.0.37]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
      at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
      at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
      at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.8.RELEASE.jar:5.2.8.RELEASE]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
      at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
      at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
      at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
      at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
      at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
      at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
      at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
  </code></pre>
  <p>Here is an error in dev tools in Chrome:</p>
  <pre><code>Angular is running in development mode. Call enableProdMode() to enable production mode.
  product-form.component.ts:44 Categories[{&quot;id&quot;:1,&quot;categoryName&quot;:&quot;cellphones&quot;},{&quot;id&quot;:2,&quot;categoryName&quot;:&quot;laptops&quot;}]
  client:52 [WDS] Live Reloading enabled.
  
  POST http://localhost:8080/products 500
  scheduleTask @ zone-evergreen.js:2845
  scheduleTask @ zone-evergreen.js:385
  onScheduleTask @ zone-evergreen.js:272
  scheduleTask @ zone-evergreen.js:378
  scheduleTask @ zone-evergreen.js:210
  scheduleMacroTask @ zone-evergreen.js:233
  scheduleMacroTaskWithCurrentZone @ zone-evergreen.js:1134
  (anonymous) @ zone-evergreen.js:2878
  proto.&lt;computed&gt; @ zone-evergreen.js:1449
  (anonymous) @ http.js:1776
  _trySubscribe @ Observable.js:42
  subscribe @ Observable.js:28
  subscribeToResult @ subscribeToResult.js:9
  _innerSub @ mergeMap.js:59
  _tryNext @ mergeMap.js:53
  _next @ mergeMap.js:36
  next @ Subscriber.js:49
  (anonymous) @ subscribeToArray.js:3
  _trySubscribe @ Observable.js:42
  subscribe @ Observable.js:28
  call @ mergeMap.js:21
  subscribe @ Observable.js:23
  call @ filter.js:13
  subscribe @ Observable.js:23
  call @ map.js:16
  subscribe @ Observable.js:23
  onSubmit @ product-form.component.ts:32
  ProductFormComponent_Template_form_ngSubmit_2_listener @ product-form.component.html:3
  executeListenerWithErrorHandling @ core.js:14316
  wrapListenerIn_markDirtyAndPreventDefault @ core.js:14351
  schedulerFn @ core.js:24822
  __tryOrUnsub @ Subscriber.js:183
  next @ Subscriber.js:122
  _next @ Subscriber.js:72
  next @ Subscriber.js:49
  next @ Subject.js:39
  emit @ core.js:24791
  onSubmit @ forms.js:4534
  NgForm_submit_HostBindingHandler @ forms.js:4567
  executeListenerWithErrorHandling @ core.js:14316
  wrapListenerIn_markDirtyAndPreventDefault @ core.js:14351
  (anonymous) @ platform-browser.js:582
  invokeTask @ zone-evergreen.js:399
  onInvokeTask @ core.js:27425
  invokeTask @ zone-evergreen.js:398
  runTask @ zone-evergreen.js:167
  invokeTask @ zone-evergreen.js:480
  invokeTask @ zone-evergreen.js:1621
  globalZoneAwareCallback @ zone-evergreen.js:1647
  Show 19 more frames
  
  
  
  ERROR HttpErrorResponse {headers: HttpHeaders, status: 500, statusText: &quot;OK&quot;, url: &quot;http://localhost:8080/products&quot;, ok: false, …}error: error: &quot;Internal Server Error&quot;message: &quot;&quot;path: &quot;/products&quot;status: 500timestamp: &quot;2020-09-05T09:15:57.997+00:00&quot;__proto__: constructor: ƒ Object()hasOwnProperty: ƒ hasOwnProperty()isPrototypeOf: ƒ isPrototypeOf()propertyIsEnumerable: ƒ propertyIsEnumerable()toLocaleString: ƒ toLocaleString()toString: ƒ ()valueOf: ƒ valueOf()__defineGetter__: ƒ __defineGetter__()__defineSetter__: ƒ __defineSetter__()__lookupGetter__: ƒ __lookupGetter__()__lookupSetter__: ƒ __lookupSetter__()get __proto__: ƒ __proto__()set __proto__: ƒ __proto__()headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}lazyInit: () =&gt; {…}arguments: (...)caller: (...)length: 0name: &quot;&quot;__proto__: ƒ ()apply: ƒ apply()arguments: (...)bind: ƒ bind()call: ƒ call()caller: (...)constructor: ƒ Function()length: 0name: &quot;&quot;toString: ƒ toString()Symbol(Symbol.hasInstance): ƒ [Symbol.hasInstance]()get arguments: ƒ ()set arguments: ƒ ()get caller: ƒ ()set caller: ƒ ()__proto__: Object[[FunctionLocation]]: &lt;unknown&gt;[[Scopes]]: Scopes[0][[FunctionLocation]]: http.js:77[[Scopes]]: Scopes[4]lazyUpdate: nullnormalizedNames: Map(0)[[Entries]]No propertiessize: (...)__proto__: Map__proto__: Objectmessage: &quot;Http failure response for http://localhost:8080/products: 500 OK&quot;name: &quot;HttpErrorResponse&quot;ok: falsestatus: 500statusText: &quot;OK&quot;url: &quot;http://localhost:8080/products&quot;__proto__: HttpResponseBase
  
         
  </code></pre>
  <p>I suspect angular fails to parse category id into the database.</p>
  <p><strong>How can i post such objects correctly? What is my mistake?</strong></p>
  <p>Here is Angular code:</p>
  <p>Product.ts</p>
  <pre><code>export class Product {
      id: string;
      brand: string;
      name: string;
      category: ProductCategory;
  }
  </code></pre>
  <p>Product-category.ts</p>
  <pre><code>export class ProductCategory {
      id: number;
      categoryName: string;
  }
  </code></pre>
  <p>Product-form.component.html</p>
  <pre><code>    &lt;div class=&quot;card my-5&quot;&gt;
      &lt;div class=&quot;card-body&quot;&gt;
        &lt;form (ngSubmit)=&quot;onSubmit()&quot; #productForm=&quot;ngForm&quot;&gt;
          &lt;div class=&quot;form-group&quot;&gt;
            &lt;label for=&quot;name&quot;&gt;Brand&lt;/label&gt;
            &lt;input type=&quot;text&quot; [(ngModel)]=&quot;product.brand&quot; 
              class=&quot;form-control&quot; 
              id=&quot;name&quot; 
              name=&quot;name&quot; 
              placeholder=&quot;Enter your name&quot;
              required #name=&quot;ngModel&quot;&gt;
          &lt;/div&gt;
          
          &lt;div class=&quot;form-group&quot;&gt;
            &lt;label for=&quot;email&quot;&gt;Name&lt;/label&gt;
            &lt;input type=&quot;text&quot; [(ngModel)]=&quot;product.name&quot; 
              class=&quot;form-control&quot; 
              id=&quot;name&quot; 
              name=&quot;name&quot; 
              placeholder=&quot;Enter your name&quot;
              required #name=&quot;ngModel&quot;&gt;
            &lt;div [hidden]=&quot;!name.pristine&quot; class=&quot;alert alert-danger&quot;&gt;name is required&lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class=&quot;form-group&quot;&gt;
            &lt;label for=&quot;category&quot;&gt;Category id&lt;/label&gt;
            &lt;select [(ngModel)]=&quot;product.category&quot; name=&quot;category.id&quot;&gt;
              &lt;option *ngFor=&quot;let category of productCategories&quot;&gt;{{ category.id }}&lt;/option&gt;
            &lt;/select&gt;
          &lt;/div&gt;
          &lt;button type=&quot;submit&quot; [disabled]=&quot;!productForm.form.valid&quot; 
            class=&quot;btn btn-info&quot;&gt;Submit&lt;/button&gt;
        &lt;/form&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  </code></pre>
  <p>Product-form.component.ts</p>
  <pre><code>   import { Component, OnInit } from '@angular/core';
  import { Product } from 'src/app/common/product';
  import { ActivatedRoute, Router } from '@angular/router';
  import { ProductService } from 'src/app/services/product.service';
  import { ProductCategory } from 'src/app/common/product-category';
  
  @Component({
    selector: 'app-product-form',
    templateUrl: './product-form.component.html',
    styleUrls: ['./product-form.component.css']
  })
  export class ProductFormComponent implements OnInit {
  
  
    product: Product;
    productCategories: ProductCategory[];
  
    constructor(private route: ActivatedRoute,
      private router: Router,
      private productService: ProductService) {
        this.product = new Product();
       }
  
    ngOnInit(): void {
      this.listProductCategories();
    }
  
    onSubmit(){
      this.productService.save(this.product).subscribe(
        result =&gt; this.gotoProductList()
      );
    }
  
    gotoProductList(): void {
     this.router.navigate(['/products']);
    }
  
    listProductCategories() {
      this.productService.getProductCategories().subscribe(
        data =&gt; {
          console.log('Categories' + JSON.stringify(data));
          this.productCategories = data;
        }
      );
  
  }
  }
  </code></pre>
  <p>product.service.ts</p>
  <pre><code>   import { Injectable } from '@angular/core';
  import { HttpClient, HttpHeaders } from '@angular/common/http';
  import { Product } from '../common/product';
  import { Observable } from 'rxjs';
  import { ProductCategory } from '../common/product-category';
  
  
  @Injectable({
    providedIn: 'root'
  })
  export class ProductService {
    
  
    private productsUrl: string;
    private categoryUrl: string;
  
    constructor(private http: HttpClient) { 
      this.productsUrl= 'http://localhost:8080/products';
      this.categoryUrl = 'http://localhost:8080/categories';
    }
  
    public findAll(): Observable&lt;Product[]&gt;{
      return this.http.get&lt;Product[]&gt;(this.productsUrl);
    }
  
    public save(product: Product){
      return this.http.post&lt;Product&gt;(this.productsUrl, product);
    }
  
    getProductCategories(): Observable&lt;ProductCategory[]&gt; {
     return this.http.get&lt;ProductCategory[]&gt;(this.categoryUrl);
    }
  }
  </code></pre>