title: Error map @OneToMany relationship with JPA and Hibernate
body: <p>I'm currently having a problem with OneToMany/ManyToOne mapping in my database. I would like my Sale Item table to have @ManyToOne mappings to Sale table.</p>
  
  <pre><code>@Entity
  @Table(name = "sale")
  public class Sale implements Serializable {
  
      private static final long serialVersionUID = -3857608619547245492L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "id")
      private Long id;
  
      @Column(name = "company", nullable = false)
      private Long company;
  
  
      @OneToMany(mappedBy = "sale", cascade = CascadeType.ALL, orphanRemoval = true)
      private List&lt;SaleItem&gt; saleItems = new ArrayList&lt;&gt;();
  
      public Sale() {
      }
  
      public void addSaleItem(SaleItem saleItem) {
          saleItems.add(saleItem);
          saleItem.setSale(this);
      }
  
      //****************************Getters and Setters****************************
  
  }
  
  
  
  @Entity
  @Table(name = "sale_item")
  public class SaleItem implements Serializable {
  
      private static final long serialVersionUID = 1016354254590870341L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "id")
      private Long id;
  
      @Column(name = "company", nullable = false)
      private Long company;    
  
      @Column(name = "description", nullable = false)
      private String description;
  
      @Column(name = "amount", nullable = false)
      private Double amount;
  
      @Column(name = "price", nullable = false)
      private Double price;
  
      @ManyToOne(fetch = FetchType.LAZY)
      private Sale sale;
  
      public SaleItem() {
  
      }
  
      //****************************Getters and Setters****************************
  
  
      public void setSale(Sale sale) {
          this.sale = sale;
      }
  
  
      @Override
      public boolean equals(Object o) {
          if (this == o)
              return true;
          if (!(o instanceof SaleItem))
              return false;
          return id != null &amp;&amp; id.equals(((SaleItem) o).getId());
      }
  
      @Override
      public int hashCode() {
          return 31;
      }   
  
      @Override
      public String toString() {
          return "SaleItem [id=" + id + ", company=" + company + ", product=" + product + ", created=" + created
                  + ", updated=" + updated + ", description=" + description + ", amount=" + amount + ", price=" + price
                  + ", discount=" + discount + ", user=" + user + ", unit=" + unit + ", sale=" + sale + "]";
      }
  
  
  }
  
      //*********************** My Controller ***********************
  
      /**
       * Create Sale
       * 
       * @param sale
       * @param result
       * @return ResponseEntity&lt;Response&lt;SaleDto&gt;&gt;
       * @throws ParseException
       */
      @PostMapping
      public ResponseEntity&lt;Response&lt;SaleDto&gt;&gt; create(@Valid @RequestBody SaleDto saleDto, BindingResult result)
              throws ParseException {
  
          log.info("Creating sale: {}", saleDto.toString());
          Response&lt;SaleDto&gt; response = new Response&lt;SaleDto&gt;();
  
          validateSale(saleDto, result);
  
          if (result.hasErrors()) {
              log.error("Error - invalid information for Sale: {}", result.getAllErrors());
              result.getAllErrors().forEach(error -&gt; response.getErrors().add(error.getDefaultMessage()));
              return ResponseEntity.badRequest().body(response);
          }
  
          response.setData(this.convertSaleDto(this.nova(saleDto, result)));
          return ResponseEntity.ok(response);
      }
  
      private Sale nova(SaleDto saleDto, BindingResult result) throws ParseException {
          SaleItem saleItem = new SaleItem();
          saleItem.setCompany(saleDto.getCompany());
          saleItem.setPrice(saleDto.getItems().get(0).getPrice());
          saleItem.setDescription(saleDto.getItems().get(0).getDescription());
          saleItem.setUnit(saleDto.getItems().get(0).getUnit());
  
          Sale sale = new Sale();
          sale.setCompany(saleDto.getCompany());
          sale.setUser(saleDto.getUser());
          sale.addSaleItem(saleItem);
  
          return this.saleService.persist(sale);
      }
  
  
  
      //**************************My Service**************
  
      /**
       * Persist a sale
       * 
       * @param Sale
       * @return Sale
       */
      Sale persist(Sale sale);
  
  
  
  //******************My impl***********************
  @Transactional
  public Sale persist(Sale sale) {
      log.info("Persisting sale {}", sale);
      return this.saleRepository.save(sale);
  }
  </code></pre>
  
  <p>But I think I did something wrong because I recevied error about id null :</p>
  
  <blockquote>
  <pre><code>SLF4J: Failed toString() invocation on an object of type [com.pissolato.api.entitys.Sale]
  Reported exception:
  java.lang.StackOverflowError
    at java.lang.AbstractStringBuilder.append(Unknown Source)
    at java.lang.StringBuilder.append(Unknown Source)
    at java.lang.StringBuilder.&lt;init&gt;(Unknown Source)
    at com.pissolato.api.entitys.SaleItem.toString(SaleItem.java:189)
    at java.lang.String.valueOf(Unknown Source)
    at java.lang.StringBuilder.append(Unknown Source)
    at java.util.AbstractCollection.toString(Unknown Source)
    at java.lang.String.valueOf(Unknown Source)
  2019-12-27 02:46:40.041  INFO 2684 --- [nio-8080-exec-2] c.p.a.services.implementation.SaleImpl   : Persisting sale [FAILED
  </code></pre>
    
    <p>toString()]
        Hibernate: 
            /* insert com.pissolato.api.entitys.Sale
                */ insert 
                into
                    sale
                    (company, created, description, discount, sub_value, type, updated, user, value) 
                values
                    (?, ?, ?, ?, ?, ?, ?, ?, ?)</p>
  </blockquote>