title: Spring Boot using save never inserts a row inside of a Postgresql table
body: <ol>
  <li>Problem Summary:</li>
  </ol>
  
  <p>I cannot save a new entry inside of my table. I followed a few tutorials before I decided to try doing this task, and everything worked. I managed to do the following tutorial perfectly while also using PostgreSQL <a href="https://www.callicoder.com/spring-boot-flyway-database-migration-example/" rel="nofollow noreferrer">https://www.callicoder.com/spring-boot-flyway-database-migration-example/</a></p>
  
  <p>The problem happened when I started using both a WebController and RestController. Using the controller.save() method isn't adding a new row.
  When I try to run it I get the following error</p>
  
  <pre><code>ERROR: duplicate key value violates unique constraint "joke_pkey"
  Detail: Key (id)=(1) already exists.
  
  Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: A different object with the same identifier value was already associated with the session : [com.example.joke4u.Joke#1]; nested exception is javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [com.example.joke4u.Joke#1]] with root cause
  </code></pre>
  
  <ol start="2">
  <li>What I’ve tried:</li>
  </ol>
  
  <p>Before anyone says it, I read somewhere that its bad practice to have both controllers in the same app but I have to do it this way because that is what my task requires me to do.
  I've tried researching around but I couldn't find anyone else with the same error.
  I even tried specifying an ID that is clearly not in use but that just updates the last row.</p>
  
  <p>I tried using both (obv not both at the same time)</p>
  
  <pre><code>@GeneratedValue(strategy = GenerationType.TABLE)
  </code></pre>
  
  <p>and </p>
  
  <pre><code>@GeneratedValue(strategy = GenerationType.AUTO)
  </code></pre>
  
  <p>Neither of them changes the outcome</p>
  
  <ol start="3">
  <li>Code:</li>
  </ol>
  
  <p>application.properties : </p>
  
  <pre><code>spring.datasource.url=jdbc:postgresql://localhost:5432/flyway_demo
  spring.datasource.username=bob
  spring.datasource.password=bob123
  
  spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
  
  spring.jpa.hibernate.ddl-auto=create
  spring.datasource.initialization-mode=always
  </code></pre>
  
  <p>My Web Controller that has the post function:</p>
  
  <pre><code>@PostMapping("/post")
  public String insertJoke(JokeForm jokeForm) {
      int categoryid = jokeForm.getCategoryId();
      String content = jokeForm.getContent();
      databasController.insert(categoryid, content);
      return "redirect:/";
  }
  </code></pre>
  
  <p>My DBController whose insert function is being called"</p>
  
  <pre><code>public Joke insert(int categoryid, String content) {
      return jokeRepository.save(new Joke(categoryid, content));
  }
  </code></pre>
  
  <p>Full Joke data class: </p>
  
  <pre><code>@Entity
  public class Joke {
  @Id
  @GeneratedValue(strategy = GenerationType.TABLE)
  @Column(columnDefinition = "serial")
  private long id;
  
  @NotNull
  private int categoryid;
  
  @NotBlank
  private String content;
  
  @Column(columnDefinition = "integer default 0")
  private int likes = 0;
  
  @Column(columnDefinition = "integer default 0")
  private int dislikes = 0;
  
  public Joke() {
  }
  
  public Joke(int categoryid, String content) {
      this.setCategoryid(categoryid);
      this.setContent(content);
  }
  
  public Joke(long id, int categoryid, String content) {
      this(categoryid, content);
      this.id = id;
  }
  
  //id
  public long getId() {
      return this.id;
  }
  
  public void setId(long id) {
      this.id = id;
  }
  
  // categoryid
  public int getCategoryid() {
      return this.categoryid;
  }
  
  public void setCategoryid(int categoryid) {
      this.categoryid = categoryid;
  }
  
  // content
  public String getContent() {
      return this.content;
  }
  
  public void setContent(String content) {
      this.content = content;
  }
  
  // likes
  public int getLikes() {
      return this.likes;
  }
  
  public void setLikes(int likes) {
      this.likes = likes;
  }
  
  public void incrementLikes() {
      ++likes;
  }
  
  public void decrementLikes() {
      --likes;
  }
  
  // dislikes
  public int getDislikes() {
      return this.dislikes;
  }
  
  public void setDislikes(int dislikes) {
      this.dislikes = dislikes;
  }
  
  public void incrementDislikes() {
      ++dislikes;
  }
  
  public void decrementDislikes() {
      --dislikes;
  }
  
  @Override
  public String toString() {
      return "{" + " id='" + getId() + "'" + ", categoryid='" + getCategoryid() + "'" + ", content='" + getContent()
              + "'" + ", likes='" + getLikes() + "'" + ", dislikes='" + getDislikes() + "'" + "}";
  }}
  </code></pre>
  
  <p>Joke Repository:</p>
  
  <pre><code>@Repository
  public interface JokeRepository extends JpaRepository&lt;Joke, Integer&gt; {
     Joke findById(long id);
     List&lt;Joke&gt; findByCategoryid(int categoryid);
  }
  </code></pre>
  
  <p>Edit:
  I found some good news! I can insert into my database, as many rows as I want, but only if I don't insert anything using a data.sql file. The task requires me to do so though :(</p>