title: Propagating foreign key with Spring Data
body: <p>I am developing an Spring Boot app based on Spring Data Rest(which uses Hibernate underneath) and mySQL database. This app is incapable to populate the foreign keys of the referenced entries(because I expect Hibernate does it for me).
  Entities:</p>
  
  <pre><code>@Entity
  public class Producto {
  
      private Integer id;
      private String nombre;
      private List&lt;Formato&gt; listaFormatos;
  
      public Producto() {
      }
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      public Integer getId() {
          return id;
      }
  
      public void setId(Integer id) {
          this.id = id;
      }
  
      public String getNombre() {
          return nombre;
      }
  
      public void setNombre(String nombre) {
          this.nombre = nombre;
      }
  
      @OneToMany(mappedBy = "producto", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
      public List&lt;Formato&gt; getListaFormatos() {
          return listaFormatos;
      }
  
      public void setListaFormatos(List&lt;Formato&gt; listaFormatos) {
          this.listaFormatos = listaFormatos;
      }
  }
  
      @Entity
  public class Formato {
  
      private Integer id;
      private Integer cantidad;
      private String unidadMedida;
      private Producto producto;
  
      public Formato() {
      }
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      public Integer getId() {
          return id;
      }
  
      public void setId(Integer id) {
          this.id = id;
      }
  
      @ManyToOne
      @JoinColumn(name = "producto_id", referencedColumnName = "id")
      public Producto getProducto() {
          return producto;
      }
  
      public void setProducto(Producto producto) {
          this.producto = producto;
      }
  
      public Integer getCantidad() {
          return cantidad;
      }
  
      public void setCantidad(Integer cantidad) {
          this.cantidad = cantidad;
      }
  
      public String getUnidadMedida() {
          return unidadMedida;
      }
  
      public void setUnidadMedida(String unidadMedida) {
          this.unidadMedida = unidadMedida;
      }
  }
  </code></pre>
  
  <p>Repository:</p>
  
  <pre><code>public interface ProductoRepository extends CrudRepository&lt;Producto, Integer&gt; {
  }
  </code></pre>
  
  <p>application.properties</p>
  
  <pre><code>spring.datasource.url = jdbc:mysql://localhost:3306/x1
  spring.datasource.username = x2
  spring.datasource.password = x3
  spring.jpa.show-sql=true
  spring.jpa.database=mysql
  
  spring.jpa.hibernate.ddl-auto=create-drop
  </code></pre>
  
  <p>pom.xml</p>
  
  <pre><code>&lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-rest&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-jersey&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-mail&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
              &lt;optional&gt;true&lt;/optional&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  </code></pre>
  
  <p>Let's say that the request I am sending in JSON format looks like this:</p>
  
  <pre><code>{"nombre": "x",
   "listaFormatos": [
      {"cantidad": 1,
       "unidadMedida":"kg"},
      {"cantidad": 2,
       "unidadMedida":"g"}
      ]
   }
  </code></pre>
  
  <p>The output of all this is that I persist an 'producto' and 2 'formato' without foreign key. Because of that, I believe I cannot bring a producto with its formats</p>
  
  <p>Does anyone know why the foreign key is not being propagated? </p>