How to validate entity in Spring Boot console application?

Vitalii :

I have a Spring Boot console application that uses Spring Data.

I have a simple jpa repository

public interface MyRepository extends JpaRepository<MyEntity, Integer> {
}

and an entity defined like this

import javax.validation.constraints.Min;
// ....

@Entity
@Table(schema = "mySchema", name = "myTable")
public class MyEntity {
    // ...
    @Column(nullable = false)
    @Min(100)
    private Integer group;
    // ...
}

When I save this entity

myRepository.save(myEntity);

pom:

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.1.8.RELEASE</version>
</parent>

<dependencies>
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-data-jpa</artifactId>
   </dependency>
   <dependency>
      <groupId>javax.validation</groupId>
      <artifactId>validation-api</artifactId>
      <version>2.0.1.Final</version>
   </dependency>
   <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-validator</artifactId>
      <version>6.1.0.Final</version>
   </dependency>
</dependencies>

I expect to see a validation error because group is 0 but I don't see any issue and at the database the value of a newly added row is 0. What else do I need to do to enable validation?

Vitalii :

Adding this to pom did the trick

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=357059&siteId=1