From 256c40aa3f02f489d3750b3b98b9c3b4c16b211c Mon Sep 17 00:00:00 2001 From: kwoyke Date: Wed, 20 Nov 2019 10:30:16 +0100 Subject: [PATCH] BAEL-3385: Hibernate @NotNull vs @Column(nullable = false) (#8194) * BAEL-3385: Hibernate @NotNull vs @Column(nullable = false) * BAEL-3385: Fix test's assertion --- .../spring-boot-persistence-h2/pom.xml | 4 +++ .../h2db/springboot/daos/ItemRepository.java | 9 ++++++ .../baeldung/h2db/springboot/models/Item.java | 18 ++++++++++++ .../src/main/resources/application.properties | 4 +++ .../com/baeldung/ItemIntegrationTest.java | 29 +++++++++++++++++++ 5 files changed, 64 insertions(+) create mode 100644 persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/daos/ItemRepository.java create mode 100644 persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/models/Item.java create mode 100644 persistence-modules/spring-boot-persistence-h2/src/test/java/com/baeldung/ItemIntegrationTest.java diff --git a/persistence-modules/spring-boot-persistence-h2/pom.xml b/persistence-modules/spring-boot-persistence-h2/pom.xml index 30c727bfc8..d1fd5b0170 100644 --- a/persistence-modules/spring-boot-persistence-h2/pom.xml +++ b/persistence-modules/spring-boot-persistence-h2/pom.xml @@ -22,6 +22,10 @@ org.springframework.boot spring-boot-starter-data-jpa + + org.springframework.boot + spring-boot-starter-validation + com.h2database h2 diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/daos/ItemRepository.java b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/daos/ItemRepository.java new file mode 100644 index 0000000000..3007f56208 --- /dev/null +++ b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/daos/ItemRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.h2db.springboot.daos; + +import com.baeldung.h2db.springboot.models.Item; +import org.springframework.data.repository.CrudRepository; + +import java.math.BigDecimal; + +public interface ItemRepository extends CrudRepository { +} diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/models/Item.java b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/models/Item.java new file mode 100644 index 0000000000..a5c5b0d3cb --- /dev/null +++ b/persistence-modules/spring-boot-persistence-h2/src/main/java/com/baeldung/h2db/springboot/models/Item.java @@ -0,0 +1,18 @@ +package com.baeldung.h2db.springboot.models; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.validation.constraints.NotNull; +import java.math.BigDecimal; + +@Entity +public class Item { + + @Id + @GeneratedValue + private Long id; + + @NotNull + private BigDecimal price; +} diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/resources/application.properties b/persistence-modules/spring-boot-persistence-h2/src/main/resources/application.properties index 109b389b58..ed1ffc63c3 100644 --- a/persistence-modules/spring-boot-persistence-h2/src/main/resources/application.properties +++ b/persistence-modules/spring-boot-persistence-h2/src/main/resources/application.properties @@ -3,6 +3,10 @@ spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.jpa.hibernate.ddl-auto=create-drop +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.format_sql=true +spring.jpa.properties.hibernate.validator.apply_to_ddl=false +#spring.jpa.properties.hibernate.check_nullability=true spring.h2.console.enabled=true spring.h2.console.path=/h2-console debug=true \ No newline at end of file diff --git a/persistence-modules/spring-boot-persistence-h2/src/test/java/com/baeldung/ItemIntegrationTest.java b/persistence-modules/spring-boot-persistence-h2/src/test/java/com/baeldung/ItemIntegrationTest.java new file mode 100644 index 0000000000..839dd87b7e --- /dev/null +++ b/persistence-modules/spring-boot-persistence-h2/src/test/java/com/baeldung/ItemIntegrationTest.java @@ -0,0 +1,29 @@ +package com.baeldung; + +import com.baeldung.h2db.springboot.SpringBootH2Application; +import com.baeldung.h2db.springboot.daos.ItemRepository; +import com.baeldung.h2db.springboot.models.Item; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.validation.ConstraintViolationException; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = SpringBootH2Application.class) +public class ItemIntegrationTest { + + @Autowired + private ItemRepository itemRepository; + + @Test + public void shouldNotAllowToPersistNullItemsPrice() { + assertThatThrownBy(() -> itemRepository.save(new Item())) + .hasRootCauseInstanceOf(ConstraintViolationException.class) + .hasStackTraceContaining("must not be null"); + } +}