0

as part of a website i'm building using Spring Boot, I receive input via Thymeleaf form. To validate the input i have created a class and annotated its fields:

package com.bank.domain;

import java.util.Date;
import javax.persistence.Column;
import javax.validation.constraints.Digits;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class NewClass {

    //if record is active/inactive (hidden)
    @Column
    private boolean active=true;

    //bank of account holder
    @NotNull
    @Size(min = 2, max =3)
    private String banknum;

    //branch
    @NotNull
    @Size(min = 1, max =3)
    private String branchnum;

    // account number
    @NotNull
    @Size(min = 4, max =10)
    private String accountnum;

    // number / range of numbers of Check
    @NotNull
    @Digits(integer=9, fraction=0)
    @Column(columnDefinition = "UNSIGNED INT(9) not null",nullable=false)
    private String fromchecknum;

    // number / range of numbers of Check
    @NotNull
    @Digits(integer=9, fraction=0)
    @Column(columnDefinition = "UNSIGNED INT(9) not null",nullable=false)
    private String tochecknum;
}

because I have debug turned on, i see that sql output in dumped to the log, even though there is no table for this class in the database. I want to not waste resources on the SQL queries or whatever they are, since there is no underlying table, just a class with fields that have validators.

So my question is - is it possible to just use a regular class and also employing the various annotation such as @Size, @Min, @NotNull, @Digits, etc?

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
Dror
  • 5,107
  • 3
  • 27
  • 45

1 Answers1

1

Looking at your class it seems that the use of @Column is the only thing dealing with persistence. Do you want to have that annotation? If you look at the package structure, @column is is persistence package and the validation related annotations reside in javax.validation and not in the persistence package. Bean validations (JSR303) like @NotNull, etc can be used even when your bean is not @Entity. I think that was your question, right?

Atul
  • 2,673
  • 2
  • 28
  • 34
  • So just by removing the @column and using a JSR303 library I'll still be able to use validations without the persistence overhead? thanks, I will give this a try... – Dror Sep 02 '17 at 14:53
  • @Dror I think yes, you should be. Let's know if you run into any problems – Atul Sep 02 '17 at 15:40