Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA @Column to prevent empty values

Is there an argument for @Column to prevent an empty String value ? Empty values are being a problem inside JLists. If not, what concise logic is the norm to be added to the entity to avoid this.

@Entity
public class Role {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(unique=true, nullable=false)
    private String roleName;
like image 466
James P. Avatar asked Jun 13 '14 21:06

James P.


2 Answers

You can use @Size annotation like this:

@Size(min=1)
@Column(unique=true, nullable=false)
private String roleName;
like image 118
Jakub H Avatar answered Oct 24 '22 13:10

Jakub H


You should use Java Bean Validation. These annotations will help you:

  • @Pattern - define a regular expression to detect empty values
  • @Size - check the size of the String.
like image 25
Moritz Petersen Avatar answered Oct 24 '22 13:10

Moritz Petersen