title: Spring Boot JPA Insert and Update
body: <p>Alright, I've looked around to find this answer for about an hour and I can't see it posted. So I bought the Spring Framework Master Class from in28minutes on Udemy. We have started implementing JPA. However, the Spring Boot versions are different( he is using 2.0.3, I am using 2.4.0). Now I know that's the issue. So the task is to simply connect to a h2 database, and interact with the data. Here is the current code setup I am using:</p>
  <p><strong>JpaDemoApplication.java</strong></p>
  <pre><code>package com.in28minutes.database.databasedemo;
  
  import java.util.Date;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.CommandLineRunner;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  
  import com.in28minutes.database.databasedemo.entity.Person;
  import com.in28minutes.database.databasedemo.jpa.PersonJpaRepository;
  
  @SpringBootApplication
  public class JpaDemoApplication implements CommandLineRunner {
  
      private Logger logger = LoggerFactory.getLogger(this.getClass());
  
      @Autowired
      PersonJpaRepository repository;
  
      public static void main(String[] args) {
          SpringApplication.run(JpaDemoApplication.class, args);
      }
  
      @Override
      public void run(String... args) throws Exception {
          
          logger.info(&quot;User id 10001 -&gt; {}&quot;, repository.findById(10001));
          
          logger.info(&quot;Inserting -&gt; {}&quot;, 
                  repository.insert(new Person(&quot;Tara&quot;, &quot;Berlin&quot;, new Date())));
          /*
          logger.info(&quot;Update 10003 -&gt; {}&quot;, 
                  repository.update(new Person(10003, &quot;Pieter&quot;, &quot;Utrecht&quot;, new Date())));
          
          //repository.deleteById(10002);
  
          logger.info(&quot;All users -&gt; {}&quot;, repository.findAll());
          */
      }
  }
  </code></pre>
  <p><strong>Person.java</strong></p>
  <pre><code>import java.util.Date;
  
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.NamedQuery;
  
  @Entity
  @NamedQuery(name=&quot;find_all_persons&quot;, query=&quot;select p from Person p&quot;)
  public class Person {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private int id;
      private String name;
      private String location;
      private Date birthDate;
  
      public Person() {}
  
      public Person(int id, String name, String location, Date birthDate) {
          super();
          this.id = id;
          this.name = name;
          this.location = location;
          this.birthDate = birthDate;
      }
  
      public Person(String name, String location, Date birthDate) {
          super();
          this.name = name;
          this.location = location;
          this.birthDate = birthDate;
      }
  
      public int getId() { return id;}
      public void setId(int id) {this.id = id;}
      public String getName() {return name;}
      public void setName(String name) {this.name = name;}
      public String getLocation() {return location;}
      public void setLocation(String location) {this.location = location;}
      public Date getBirthDate() {return birthDate;}
      public void setBirthDate(Date birthDate) {this.birthDate = birthDate;}
      @Override
      public String toString() {
          return String.format(&quot;\nPerson [id=%s, name=%s, location=%s, birthDate=%s]&quot;, id, name, location, birthDate);
      }
  }
  </code></pre>
  <p><strong>PersonJpaRepository.java</strong></p>
  <pre><code>package com.in28minutes.database.databasedemo.jpa;
  
  import java.util.List;
  
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  import javax.persistence.TypedQuery;
  import javax.transaction.Transactional;
  
  import org.springframework.stereotype.Repository;
  
  import com.in28minutes.database.databasedemo.entity.Person;
  
  @Repository
  @Transactional
  public class PersonJpaRepository {
  
      // connect to the database
      @PersistenceContext
      EntityManager entityManager;
  
      public List&lt;Person&gt; findAll() {
          TypedQuery&lt;Person&gt; namedQuery = entityManager.createNamedQuery(&quot;find_all_persons&quot;, Person.class);
          return namedQuery.getResultList();
      }
  
      public Person findById(int id) {
          return entityManager.find(Person.class, id);// JPA
      }
  
      public Person update(Person person) {
          return entityManager.merge(person);
      }
  
      public Person insert(Person person) {
          return entityManager.merge(person);
      }
  
      public void deleteById(int id) {
          Person person = findById(id);
          entityManager.remove(person);
      }
  
  }
  </code></pre>
  <p><strong>application.properties</strong></p>
  <pre><code>spring.h2.console.enabled=true
  spring.jpa.show-sql=true
  #logging.level.root=debug
  #spring.jpa.hibernate.use-new-id-generator-mappings=false
  spring.jpa.hibernate.ddl-auto=none
  spring.datasource.url=jdbc:h2:mem:testdb
  spring.data.jpa.repositories.bootstrap-mode=default
  </code></pre>
  <p><strong>data.sql</strong></p>
  <pre><code>INSERT INTO PERSON (ID, NAME, LOCATION, BIRTH_DATE)
  VALUES(10001, 'Ranga', 'Hyderabad', sysdate());
  INSERT INTO PERSON (ID, NAME, LOCATION, BIRTH_DATE)
  VALUES(10002, 'James', 'New York', sysdate());
  INSERT INTO PERSON (ID, NAME, LOCATION, BIRTH_DATE)
  VALUES(10003, 'Pieter', 'Amsterdam', sysdate());
  </code></pre>
  <p><strong>schema.sql</strong></p>
  <pre><code>create table person
  (
      id integer not null,
      name varchar(255) not null,
      location varchar(255),
      birth_date timestamp,
      primary key(id)
  );
  </code></pre>
  <p><strong>pom.xml</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
      xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &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.4.0&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
      &lt;groupId&gt;com.in28minutes.database&lt;/groupId&gt;
      &lt;artifactId&gt;database-demo&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;database-demo&lt;/name&gt;
      &lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
      &lt;properties&gt;
          &lt;java.version&gt;15&lt;/java.version&gt;
      &lt;/properties&gt;
      &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-jdbc&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;com.h2database&lt;/groupId&gt;
              &lt;artifactId&gt;h2&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-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  <p>I know this is a lot, but I am very new to Spring, and I HAVE to have this down by January, due to me starting a new job. The specific error is that the table is created, the first select by id works, but the insert passes a null value:</p>
  <pre><code>Hibernate: insert into person (id, birth_date, location, name) values (null, ?, ?, ?)
  2020-12-03 10:59:16.724  WARN 14464 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 23502, SQLState: 23502
  2020-12-03 10:59:16.724 ERROR 14464 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : NULL not allowed for column &quot;ID&quot;; SQL statement:
  insert into person (id, birth_date, location, name) values (null, ?, ?, ?) [23502-200]
  </code></pre>
  <p>I have tried 5 different suggestions, but none have worked. What am I doing wrong? Pointing to something that can explain how to do it works as well, I'm always looking for more current learning resources.</p>