0

I want to use spring mvc-hibernate-hibernate validator in my project. But in the class I applied the anotations, validation and mysql anotations are overlapping. The use of these two is normally the case. Or is it done in another way?

@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
Integer id;

@Column(name="firstname")
@NotBlank(message="Name mustn't be empty.")
@Size(max=20,min=2,message="Your name must be between 2-20.")
@Pattern(regexp ="([a-zA-Z]+)",message="You can't enter number or symols or Turkish caracter.")
String firstname;

@Column(name="lastname")
@NotBlank(message="Last name mustn't be empty.")
@Size(max=20,min=2,message="Your lastname must be between 2-20.")
@Pattern(regexp ="([a-zA-Z]+)",message="You can't enter number or symols or Turkish caracter.")
String lastname;

@Column(name="date")
@NotBlank
@DateTimeFormat(pattern="yyyy-mm-dd")
@DateValid
String date;
  • You can use spring mvc, hibernate, hibernate validator together. There is no problem. what's your error bro? – Mohsen Dec 05 '18 at 21:42
  • I've found out where the fault is, but I'm gonna ask you something. Wouldn't we have created code confusion using so many anotations? – Emrullah Ozkilinc Dec 05 '18 at 23:22
  • usually we use annotations and it's ok let me put an answer for this question with complete explanation for you – Mohsen Dec 05 '18 at 23:25
  • Please read my answer and make it accepted if it was helpful :) – Mohsen Dec 05 '18 at 23:53

1 Answers1

0

You can use spring mvc, hibernate, hibernate validator together and there is no conflict. There is a good project that named JHipster and you can see how to use these technologies to gether.

Wouldn't we have created code confusion using so many anotations?

This is jhipster user class:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;

@NotNull
@Pattern(regexp = Constants.LOGIN_REGEX)
@Size(min = 1, max = 50)
@Column(length = 50, unique = true, nullable = false)
private String login;

@JsonIgnore
@NotNull
@Size(min = 60, max = 60)
@Column(name = "password_hash", length = 60, nullable = false)
private String password;

@Size(max = 50)
@Column(name = "first_name", length = 50)
private String firstName;

@Size(max = 50)
@Column(name = "last_name", length = 50)
private String lastName;

@Email
@Size(min = 5, max = 254)
@Column(length = 254, unique = true)
private String email;

@NotNull
@Column(nullable = false)
private boolean activated = false;

Usually there are two types of configuration in java

  • Annotations
  • XML configuration

For more information visit annotation vs xml

Mohsen
  • 4,536
  • 2
  • 27
  • 49