title: JHipster Foreign Key in the child table with a bidirectional One to Many relationship is Null
body: <p>jdl file:</p>
  
  <pre><code>entity Box{
  boxNum String maxlength(40)
  }
  
  entity BoxInventory{
  item String maxlength(40)
  quantity BigDecimal
  }
  
  relationship OneToMany {
  Box to BoxInventory{box}
  } 
  </code></pre>
  
  <p>This jdl file generated the following classes. I modified the generated class to add CascadeType.ALL and FetchType.LAZY in the generated model classes.</p>
  
  <pre><code>@Entity
  @Table(name = "box")
  @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
  @Document(indexName = "box")
  public class Box implements Serializable {
  
  private static final long serialVersionUID = 1L;
  
  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
  @SequenceGenerator(name = "sequenceGenerator")
  private Long id;
  
  @Size(max = 40)
  @Column(name = "box_num", length = 40)
  private String boxNum;
  
  @OneToMany(mappedBy = "box", cascade = CascadeType.ALL, orphanRemoval = true)
  @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
  private Set&lt;BoxInventory&gt; boxInventories = new HashSet&lt;&gt;();
  
  // jhipster-needle-entity-add-field - JHipster will add fields here, do not remove
  public Long getId() {
      return id;
  }
  
  public void setId(Long id) {
      this.id = id;
  }
  
  public String getBoxNum() {
      return boxNum;
  }
  
  public Box boxNum(String boxNum) {
      this.boxNum = boxNum;
      return this;
  }
  
  public void setBoxNum(String boxNum) {
      this.boxNum = boxNum;
  }
  
  public Set&lt;BoxInventory&gt; getBoxInventories() {
      return boxInventories;
  }
  
  public Box boxInventories(Set&lt;BoxInventory&gt; boxInventories) {
      this.boxInventories = boxInventories;
      return this;
  }
  
  public Box addBoxInventory(BoxInventory boxInventory) {
      this.boxInventories.add(boxInventory);
      boxInventory.setBox(this);
      return this;
  }
  
  public Box removeBoxInventory(BoxInventory boxInventory) {
      this.boxInventories.remove(boxInventory);
      boxInventory.setBox(null);
      return this;
  }
  
  public void setBoxInventories(Set&lt;BoxInventory&gt; boxInventories) {
      this.boxInventories = boxInventories;
  }
  // jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here, do not remove
  
  @Override
  public boolean equals(Object o) {
      if (this == o) {
          return true;
      }
      if (o == null || getClass() != o.getClass()) {
          return false;
      }
      Box box = (Box) o;
      if (box.getId() == null || getId() == null) {
          return false;
      }
      return Objects.equals(getId(), box.getId());
  }
  
  @Override
  public int hashCode() {
      return Objects.hashCode(getId());
  }
  
  @Override
  public String toString() {
      return "Box{" +
          "id=" + getId() +
          ", boxNum='" + getBoxNum() + "'" +
          "}";
  }
  }
  
  @Entity
  @Table(name = "box_inventory")
  @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
  @Document(indexName = "boxinventory")
  public class BoxInventory implements Serializable {
  
  private static final long serialVersionUID = 1L;
  
  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
  @SequenceGenerator(name = "sequenceGenerator")
  private Long id;
  
  @Size(max = 40)
  @Column(name = "item", length = 40)
  private String item;
  
  @Column(name = "quantity", precision = 10, scale = 2)
  private BigDecimal quantity;
  
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "box_id", nullable = false)
  @JsonIgnoreProperties("boxInventories")
  private Box box;
  
  // jhipster-needle-entity-add-field - JHipster will add fields here, do not remove
  public Long getId() {
      return id;
  }
  
  public void setId(Long id) {
      this.id = id;
  }
  
  public String getItem() {
      return item;
  }
  
  public BoxInventory item(String item) {
      this.item = item;
      return this;
  }
  
  public void setItem(String item) {
      this.item = item;
  }
  
  public BigDecimal getQuantity() {
      return quantity;
  }
  
  public BoxInventory quantity(BigDecimal quantity) {
      this.quantity = quantity;
      return this;
  }
  
  public void setQuantity(BigDecimal quantity) {
      this.quantity = quantity;
  }
  
  public Box getBox() {
      return box;
  }
  
  public BoxInventory box(Box box) {
      this.box = box;
      return this;
  }
  
  public void setBox(Box box) {
      this.box = box;
  }
  // jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here, do not remove
  
  @Override
  public boolean equals(Object o) {
      if (this == o) {
          return true;
      }
      if (o == null || getClass() != o.getClass()) {
          return false;
      }
      BoxInventory boxInventory = (BoxInventory) o;
      if (boxInventory.getId() == null || getId() == null) {
          return false;
      }
      return Objects.equals(getId(), boxInventory.getId());
  }
  
  @Override
  public int hashCode() {
      return Objects.hashCode(getId());
  }
  
  @Override
  public String toString() {
      return "BoxInventory{" +
          "id=" + getId() +
          ", item='" + getItem() + "'" +
          ", quantity=" + getQuantity() +
          "}";
  }
  }
  
  @SuppressWarnings("unused")
  @Repository
  public interface BoxRepository extends JpaRepository&lt;Box, Long&gt; {
  }
  
  public interface BoxSearchRepository extends ElasticsearchRepository&lt;Box, Long&gt; {
  }
  
  @RestController
  @RequestMapping("/api")
  public class BoxResource {
  
  private final Logger log = LoggerFactory.getLogger(BoxResource.class);
  
  private static final String ENTITY_NAME = "modelBox";
  
  private final BoxService boxService;
  
  public BoxResource(BoxService boxService) {
      this.boxService = boxService;
  }
  
  /**
   * POST  /boxes : Create a new box.
   *
   * @param box the box to create
   * @return the ResponseEntity with status 201 (Created) and with body the new box, or with status 400 (Bad Request) if the box has already an ID
   * @throws URISyntaxException if the Location URI syntax is incorrect
   */
  @PostMapping("/boxes")
  @Timed
  public ResponseEntity&lt;Box&gt; createBox(@Valid @RequestBody Box box) throws URISyntaxException {
      log.debug("REST request to save Box : {}", box);
      if (box.getId() != null) {
          throw new BadRequestAlertException("A new box cannot already have an ID", ENTITY_NAME, "idexists");
      }
      Box result = boxService.save(box);
      return ResponseEntity.created(new URI("/api/boxes/" + result.getId()))
          .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
          .body(result);
  }
  }
  </code></pre>
  
  <p>The problem is, when I invoke createBox using the following input:</p>
  
  <pre><code>{
    "boxInventories": [
      {
        "item": "I1",
        "quantity": 10
      }
    ],
    "boxNum": "B2"
  }
  </code></pre>
  
  <p>Records are inserted in both the Box and Box_Inventory tables. However, the box_id column in the Box_Inventory table is null. </p>
  
  <p>This is what I see in the logs:</p>
  
  <pre><code>2018-09-22 12:10:41.375 DEBUG 1596 --- [  XNIO-2 task-1] c.s.inventory.aop.logging.LoggingAspect  : Enter: com.sb.inventory.web.rest.BoxResource.createBox() with argument[s] = [Box{id=null, boxNum='B2'}]
  2018-09-22 12:10:41.389 DEBUG 1596 --- [  XNIO-2 task-1] com.sb.inventory.web.rest.BoxResource    : REST request to save Box : Box{id=null, boxNum='B2'}
  2018-09-22 12:10:41.412 DEBUG 1596 --- [  XNIO-2 task-1] c.s.inventory.aop.logging.LoggingAspect  : Enter: com.sb.inventory.service.impl.BoxServiceImpl.save() with argument[s] = [Box{id=null, boxNum='B2'}]
  2018-09-22 12:10:41.425 DEBUG 1596 --- [  XNIO-2 task-1] c.s.i.service.impl.BoxServiceImpl        : Request to save Box : Box{id=null, boxNum='B2'}
  Hibernate: select nextval ('hibernate_sequence')
  Hibernate: select nextval ('hibernate_sequence')
  2018-09-22 12:10:42.341 DEBUG 1596 --- [  XNIO-2 task-1] c.s.inventory.aop.logging.LoggingAspect  : Exit: com.sb.inventory.service.impl.BoxServiceImpl.save() with result = Box{id=2401, boxNum='B2'}
  Hibernate: insert into box (box_num, id) values (?, ?)
  Hibernate: insert into box_inventory (box_id, item, quantity, id) values (?, ?, ?, ?)
  2018-09-22 12:10:42.505 DEBUG 1596 --- [  XNIO-2 task-1] c.s.inventory.aop.logging.LoggingAspect  : Exit: com.sb.inventory.web.rest.BoxResource.createBox() with result = &lt;201 Created,Box{id=2401, boxNum='B2'},{Location=[/api/boxes/2401], X-modelApp-alert=[A new modelBox is created with identifier 2401], X-modelApp-params=[2401]}&gt;
  </code></pre>
  
  <p>I read quite a few resources on stackoverflow and other blogs. All suggest the approach taken should work (meaning the foreign key should have been captured). However, somehow, the foreign key is captured as null. and the box inventory record is not linked to the box record.</p>