I am currently playing Spring boot 1.4.2, which I pulled into Spring-boot-starter-web and Spring-boot-starter-jpa.
My main problem is that when I save a new object, it works fine (everything's cool).
However, if I save a new product object with the same identifier (for example, a duplicate element), it does not throw an exception. I was expecting a ConstrintViolationException or something similar.
Given the following setting:
Application.java
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }
ProductRepository.java
@Repository public interface ProductRepository extends JpaRepository<Product, String> {}
Jpaconfig.java
@Configuration @EnableJpaRepositories(basePackages = "com.verric.jpa.repository" ) @EntityScan(basePackageClasses ="com.verric.jpa") @EnableTransactionManagement public class JpaConfig { @Bean JpaTransactionManager transactionManager() { return new JpaTransactionManager(); } }
Note. JpaConfig.java and Application.java are in the same package.
ProductController.java
@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()); } } }
and finally Product.java
@Entity(name = "product") @Getter @Setter @AllArgsConstructor @EqualsAndHashCode(of = "id") public class Product { protected Product() { } @Id private String id; @Column private String name; @Column private Long price; @Column private Boolean taxable; }
Getter, setter and equalsHashcode .. are lombok annotations.
Miscellanea:
Spring boot: 1.4.2
Hibernate ORM: 5.2.2.FINAL
This issue occurs regardless of whether I comment on the controller with or without @Transactional
Base db explicitly throws exception
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
I know that itβs better (more often) to break the data access material into your own service level instead of dumping it into the controller
Controller semantics are not REST
What I tried:
Spring CrudRepository Exceptions
I tried to execute the answer from this question, unfortunately my code never falls into DataAccesException
Does Spring JPA give an error if the save function is unsuccessful?
Again a similar answer to the question above.
http://www.baeldung.com/spring-dataIntegrityviolationexception
I tried adding a bean to my JPAconfig.java class, which:
@Bean public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){ return new PersistenceExceptionTranslationPostProcessor(); }
But nothing happened.
Sorry for the long post, enter in advance