title: Auto generate id in Spring MVC
body: <p>I'm trying to save data collected by a JSP page within a database (Postgres). At first I tried to insert any values manually (including <code>id</code>) in a form and I had no problem saving the data in my db. Now I'm trying to auto generate the <code>id</code> values, but I'm a little bit confused about how to do it properly. </p>
  
  <p><strong>My model - Product</strong></p>
  
  <pre><code>public class Product {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
      private String description;
      private double price;
      @DateTimeFormat(pattern = "dd/MM/yyyy")
      private Date date;
  
      public Product() { }
  
      public Product(Long id, String description, double price, Date date) {
          this.id = id;
          this.description = description;
          this.price = price;
          this.date = date;
      }
      //getters and setters
  }
  </code></pre>
  
  <p><strong>My controller - DBConnection</strong></p>
  
  <pre><code>@Controller
  public class DBConnection {
  
      @Autowired
      private ProductDao prDao;
  
      @RequestMapping(value = "/newproduct", method = RequestMethod.GET)
      public ModelAndView showForm() {
          Product product = new Product();
          return new ModelAndView("newproduct", "product", product);
      }
  
      @RequestMapping(value = "/newproduct", method = RequestMethod.POST)
      public ModelAndView submitForm(@ModelAttribute("product") Product product) {
          ModelAndView mav = new ModelAndView();
  
          prDao.addProduct(product.getId(), product.getDescription(), product.getPrice(), product.getDate());
          mav.setViewName("product");
          mav.addObject("product", product);
          return mav;
      }
  }
  </code></pre>
  
  <p><strong>ProductDaoImpl</strong></p>
  
  <pre><code>public class ProductDaoImpl implements ProductDao {
      private DataSource dataSource;
      private JdbcTemplate jdbcTemplate;
  
      @Override
      public void setDataSource(DataSource ds) {
          this.dataSource = ds;
          this.jdbcTemplate = new JdbcTemplate(dataSource);
  
      }
  
      @Override
      public void addProduct(Long id, String description, double price, Date date) {
          jdbcTemplate.update("INSERT INTO products values(?, ?, ?, ?)", id, description, price, date);
      }
  }
  </code></pre>
  
  <p><strong>My form in newproduct.jsp</strong></p>
  
  <pre><code>&lt;form:form method="POST" action="newproduct" modelAttribute="product"&gt;
      &lt;table&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;form:label path="description"&gt;Description&lt;/form:label&gt;&lt;/td&gt;
              &lt;td&gt;&lt;form:input path="description" /&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;form:label path="price"&gt;Price&lt;/form:label&gt;&lt;/td&gt;
              &lt;td&gt;&lt;form:input path="price" /&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;form:label path="date"&gt;Date (dd/mm/yyyy)&lt;/form:label&gt;&lt;/td&gt;
              &lt;td&gt;&lt;form:input path="date" /&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;input type="submit" value="Submit" /&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/form:form&gt;
  </code></pre>
  
  <p><strong>Error</strong> when I submit the form</p>
  
  <pre><code>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [INSERT INTO products values(?, ?, ?, ?)]; ERROR: null values in column "id" violates not-null constraint
    Detail: The row contains error (null, nnvfe, 10.00, 2010-10-10).; nested exception is org.postgresql.util.PSQLException: ERROR: null values in column "id" violates not-null constraint
    Detail: The row contains error (null, nnvfe, 10.00, 2010-10-10).
  </code></pre>
  
  <p>I think the problem is in the <code>addProduct</code> method: the <code>id</code> value isn't yet created when I tried to get it. </p>
  
  <p>How can I implements an auto generated id? Is the JPA annotation the correct way to do that thing?</p>