Jono
Jono

Reputation: 18108

what exceptions get thrown for these annotations in java?

As the title says, what exceptions get thrown under these conditions below on my persisted java class:

@Column(name = "USERNAME", nullable=false, unique=true)
private String username;


@Column(name = "PASSWORD")
@NotNull
@Size(min = 5, max = 25)
private String password;

What is the difference between using @NotNull and @Column(nullable=false)?

i could not find any api docs explaining this along with the type of exception that can occur if username is null and not unique. And what gets thrown if password is null, less then chars and more then 25 chars.

Upvotes: 1

Views: 4570

Answers (1)

skaffman
skaffman

Reputation: 403481

javax.persistence.Column is used to specify the details of the database column. The nullable attribute is generally only used when generating the table definitions, and not used at runtime for validation.

javax.validation.constraints.NotNull is used at runtime to validate the data before persisting it, assuming that a validation provider is enabled and configured. Violations will throw a subclass of ValidationException.

the type of exception that can occur if username is null and not unique

No exception will be thrown by the application itself, but if the database's table definitions were generated from these annotations, then some JPA exception will be thrown.

And what gets thrown if password is null, less then chars and more then 25 chars.

A ValidationException of some form.

Upvotes: 4

Related Questions