What’s the happens if you try to insert null-value into column constraint not null at H2 DB
Make some codes as entity, repository and application.yml (includes H2 database info).
1 2 3 4 5 6 7 8 9 10 |
@Data @Entity public class Member { @Id @GeneratedValue private Long id; @Column(nullable = false) private String name; } |
1 2 |
public interface MemberRepository extends JpaRepository<Member, Long> { } |
1 2 3 4 5 6 7 8 9 |
spring: h2: console: enabled: true jpa: show-sql: true datasource: hikari: jdbc-url: jdbc:h2:mem:testdb;MODE=MySQL; # MODE=MySQL is the point |
And then, you try to run the test bellow.
1 2 3 4 5 6 7 8 9 10 11 12 |
@SpringBootTest class DemoApplicationTests { @Autowired private MemberRepository memberRepository; @Test void test() { memberRepository.save(new Member()); // try to insert null-value into the database System.out.println(memberRepository.findAll()); } } |
The result of the test is success. It’s contrary to what you expected. The column… Read More »What’s the happens if you try to insert null-value into column constraint not null at H2 DB