Spring JpaRepositroy.save() does not appear to throw exception on duplicate saves
<p>I'm currently playing around on Spring boot 1.4.2 in which I've pulled in Spring-boot-starter-web and Spring-boot-starter-jpa.</p>

<p>My main issue is that when I save a new entity it works fine (all cool).</p>

<p>However if I save a new product entity with the same id (eg a duplicate entry), it does not throw an exception. I was expecting ConstrintViolationException or something similar.</p>

<p>Given the following set up:</p>

<p>Application.java</p>

<pre><code>@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
</code></pre>

<p>ProductRepository.java</p>

<pre><code>@Repository
public interface ProductRepository extends JpaRepository&lt;Product, String&gt; {}
</code></pre>

<p>JpaConfig.java</p>

<pre><code>@Configuration
@EnableJpaRepositories(basePackages = "com.verric.jpa.repository" )
@EntityScan(basePackageClasses ="com.verric.jpa")
@EnableTransactionManagement
public class JpaConfig {

    @Bean
    JpaTransactionManager transactionManager() {
        return new JpaTransactionManager();
    }
}
</code></pre>

<p>Note JpaConfig.java and Application.java are in the same package.</p>

<p>ProductController.java</p>

<pre><code>@RestController
@RequestMapping(path = "/product")
public class ProductController {

    @Autowired
    ProductRepository productRepository;

    @PostMapping("createProduct")
    public void handle(@RequestBody @Valid CreateProductRequest request) {
        Product product = new Product(request.getId(), request.getName(), request.getPrice(), request.isTaxable());
        try {
            productRepository.save(product);
        } catch (DataAccessException ex) {
            System.out.println(ex.getCause().getMessage());
        }
    }
}
</code></pre>

<p>and finally Product.java</p>

<pre><code>@Entity(name = "product")
@Getter
@Setter
@AllArgsConstructor
@EqualsAndHashCode(of = "id")
public class Product {

    protected Product() { /* jpa constructor*/ }

    @Id
    private String id;

    @Column
    private String name;

    @Column
    private Long price;

    @Column
    private Boolean taxable;
}
</code></pre>

<p>The getter, setter and equalsHashcode.. are <a href="https://projectlombok.org/" rel="noreferrer">lombok</a> annotations.</p>

<p><strong>Miscellaneous:</strong></p>

<p>Spring boot : 1.4.2</p>

<p>Hibernate ORM: 5.2.2.FINAL</p>

<p>This issue happens regardless if I annotate the controller with or without <code>@Transactional</code></p>

<p>The underlying db shows the exception clearly </p>

<pre><code>2016-11-15 18:03:49 AEDT [40794-1] verric@stuff ERROR:  duplicate key value violates unique constraint "product_pkey"
2016-11-15 18:03:49 AEDT [40794-2] verric@stuff DETAIL:  Key (id)=(test001) already exists
</code></pre>

<p>I know that is better (more common) to break the data access stuff into its own service layer instead of dumping it in the controller</p>

<p>The semantics of the controller aren't ReST</p>

<p><strong>Things I've tried:</strong></p>

<p><a href="https://stackoverflow.com/questions/23325413/spring-crudrepository-exceptions">Spring CrudRepository exceptions</a></p>

<p>I've tried implementing the answer from this question, unfortunately my code never ever hits the DataAccesException exception </p>

<p><a href="https://stackoverflow.com/questions/25266248/does-spring-jpa-throw-an-error-if-save-function-is-unsuccessful">Does Spring JPA throw an error if save function is unsuccessful?</a></p>

<p>Again similar response to the question above.</p>

<p><a href="http://www.baeldung.com/spring-dataIntegrityviolationexception" rel="noreferrer">http://www.baeldung.com/spring-dataIntegrityviolationexception</a></p>

<p>I tried adding the bean to my JPAconfig.java class that is:</p>

<pre><code>   @Bean
   public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
      return new PersistenceExceptionTranslationPostProcessor();
   }
</code></pre>

<p>But nothing seemed to happen.</p>

<p>Sorry for long post, ty in advance</p>