0

I have used @NotNull annotation on Java bean class for JSF in register page. But it is not working as expceted.

Here is my xhtml page part :

User Name : <h:inputText value="#{user.userName}" />

And here is my Java bean part :

@NotNull(message = "User name cannot be null")
private String userName;

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

But it still save userName to database(PostgreSQL) as ''. So technically it is not null. I know may be I must also make not null that column on database. But what for we use @NotNull annotation then?

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
berkancetin
  • 315
  • 1
  • 3
  • 18
  • 1
    Possible duplicate of [h:inputText which is bound to String property is submitting empty string instead of null](https://stackoverflow.com/questions/2203322/hinputtext-which-is-bound-to-string-property-is-submitting-empty-string-instead) – Kukeltje May 12 '19 at 06:13

1 Answers1

2

I believe what you want to use is @NotBlank. Here is some reference between @NotNull,@NotEmpty, and @NotBlank.

public class UserNotBlank {

    @NotBlank(message = "Name may not be blank")
    private String name;

    // standard constructors / getters / toString

}

@NotNull: a constrained CharSequence, Collection, Map, or Array is valid as long as it’s not null, but it can be empty

@NotEmpty: a constrained CharSequence, Collection, Map, or Array is valid as long as it’s not null and its size/length is greater than zero

@NotBlank: a constrained String is valid as long as it’s not null and the trimmed length is greater than zero

Chris
  • 90
  • 10
  • `@NotNull` still can be used: https://stackoverflow.com/questions/2203322/hinputtext-which-is-bound-to-string-property-is-submitting-empty-string-instead – Kukeltje May 12 '19 at 06:15