title: Spring Data JPA does not persist the oneToMany list
body: <p>I am creating a new project and using Spring Data JPA to create some REST endpoints.</p>
  
  <pre><code>&lt;parent&gt;
  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
  &lt;version&gt;2.1.6.RELEASE&lt;/version&gt;
  &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
  &lt;/parent&gt;
  </code></pre>
  
  <p>I am able to put and persist to my primary class (customer), which works as long as the json file does not have any oneToMany data.  However, when posting to customer, if there is oneToMany data I am getting errors.</p>
  
  <p>The errors relate to the foreign key being null when trying to persist.  I am not sure how Spring Data JPA should be using the annotation to let hibernate know what the value of the foreign key should be.</p>
  
  <p>I have looked at numerous bi-directional OneToMany examples, as well as examples for creating foreign keys and have tried a number of modifications without success.</p>
  
  <p>I also tried to use the spring.jpa.hibernate.ddl-auto=update to help create and update the database schema without any luck.</p>
  
  <p>The customer</p>
  
  <pre><code>@Entity
  @Table(name="customer")
  @EntityListeners(AuditingEntityListener.class)
  public class Customer extends Auditable&lt;String&gt; {
  
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  @Column(name="id")
  private int id;
  @Column(name="first_name")
  private String firstName;
  @Column(name="last_name")
  private String lastName;
  
  @OneToMany(fetch=FetchType.LAZY, mappedBy="customer", cascade={CascadeType.ALL})
  private List&lt;EmailAddress&gt; emailAddresses;
  .......
  </code></pre>
  
  <p>The emails</p>
  
  <pre><code>@Table(name="email_address")
  @EntityListeners(AuditingEntityListener.class)
  public class EmailAddress extends Auditable&lt;String&gt; {
  
  @Id
  @GeneratedValue(strategy=GenerationType.IDENTITY)
  @Column(name="id")
  private int id;
  @Column(name="email_type")
  private byte emailType;
  @Column(name="email")
  private String email;
  
  @ManyToOne(fetch=FetchType.LAZY, cascade={CascadeType.ALL})
  @JoinColumn(name="customer_id")
  @JsonIgnore
  private Customer customer;
  .....
  </code></pre>
  
  <p>The postman json test</p>
  
  <pre><code>{
      "id": 1,
      "firstName": "Bobby",
      "lastName": "Smith",
      "emailAddresses": [
          {
              "id": 1,
              "emailType": 1,
              "email": "bobby@bobby.com",
          },
          {
              "id": 2,
              "emailType": 1,
              "email": "bobby@gmail.com",
          }
      ]
  
  }
  </code></pre>
  
  <p>BTW, I have confirmed that within the customer controller, that the emails are included in the request body of customer.</p>
  
  <p>The customer controller</p>
  
  <pre><code>@PutMapping("/customers")
  public Customer updateCustomer(@RequestBody Customer theCustomer) {
  
  System.out.println("****email count "+theCustomer.getEmailAddresses().size());
  for(EmailAddress index: theCustomer.getEmailAddresses()) {
  System.out.println(index.toString());
  }
  
  customerService.save(theCustomer);
  
  return theCustomer;
  }
  </code></pre>
  
  <p>The customer service</p>
  
  <pre><code>@Override
  public void save(Customer theCustomer) {
  //Validate the input
  if(theCustomer == null) {
  throw new CustomerNotFoundException("Did not find the Customer, was null...");
  }
  customerRepository.save(theCustomer);
  }
  </code></pre>
  
  <p>MySQL Script</p>
  
  <pre><code>--
  -- Table structure for table `customer`
  --
  DROP TABLE IF EXISTS `customer`;
  CREATE TABLE `customer` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(24) COLLATE utf8_bin NOT NULL,
  `last_name` varchar(24) COLLATE utf8_bin NOT NULL,
  PRIMARY KEY (`id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='Primary Customer Table';
  
  --
  -- Table structure for table `email_address`
  --
  DROP TABLE IF EXISTS `email_address`;
  CREATE TABLE `email_address` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `email_type` tinyint(4) unsigned NOT NULL COMMENT 'email type',
    `email` varchar(128) COLLATE utf8_bin NOT NULL COMMENT 'email address',
    `customer_id` int(11) NOT NULL COMMENT 'foreign key',
    INDEX par_ind (customer_id),
    PRIMARY KEY (`id`),
    UNIQUE KEY `email` (`email`),
    KEY FK_EMAIL_CUSTOMER_idx (customer_id),
    CONSTRAINT FK_EMAIL_CUSTOMER FOREIGN KEY (customer_id) REFERENCES customer (id) ON DELETE CASCADE ON UPDATE CASCADE
  ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='email addresses';
  </code></pre>
  
  <p>Postman Complaint</p>
  
  <pre><code>{
      "status": 400,
      "message": "could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement",
      "timeStamp": 1566840491483
  }
  </code></pre>
  
  <p>Console Complaint</p>
  
  <pre><code>****email count 2
  EmailAddress [id=1, type=1, email=bobby@bobby.com]
  EmailAddress [id=2, type=1, email=bobby@gmail.com]
  2019-08-28 17:33:07.625  WARN 8669 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1048, SQLState: 23000
  2019-08-28 17:33:07.626 ERROR 8669 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : Column 'customer_id' cannot be null
  2019-08-28 17:33:07.629 ERROR 8669 --- [nio-8080-exec-2] o.h.i.ExceptionMapperStandardImpl        : HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
  2019-08-28 17:33:07.735  WARN 8669 --- [nio-8080-exec-2] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement]
  </code></pre>
  
  <p>Therefore, with a post or put, I am not sure why the Spring Data JPA save does not satisfy the foreign key constraint for entities with oneToMany relationships.  I am guessing it is either some missing annotations or something wrong with my sql script.  Not sure why the update data does not persist to the email_address table.  Does the emailAddress entity require some type of getter/setter for customer_id?</p>