Hibernate is a practical Hibernate topic that becomes clear when you connect the definition to a small working example.
Use this page to understand what happens, why it happens, how to verify it, and what mistake usually breaks the concept.
After reading, practice Hibernate with a normal case, a boundary case, and a broken case so the idea becomes usable instead of memorized.
Hibernate Validation @NotNull @Size @Email should be studied as a practical Hibernate lesson, not as a label. Start by naming the input, the rule that changes the input, and the result a learner should be able to predict after reading the page.
In the hibernate > validation page, the notes should connect the definition with a working scenario, a mistake that beginners actually make, and the exact check that proves the fix. That makes the topic useful for coding, debugging, and interview revision.
Hibernate Validator is the reference implementation of Jakarta Bean Validation (formerly javax.validation). It allows you to declare constraints directly on entity fields using annotations. Spring Boot includes it automatically via spring-boot-starter-validation.
package com.example.model;
import jakarta.persistence.*;
import jakarta.validation.constraints.*;
@Entity
@Table(name = "users")
public class User {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull(message = "Name is required")
@NotBlank(message = "Name cannot be blank")
@Size(min = 2, max = 50, message = "Name must be between 2 and 50 characters")
private String name;
@NotNull(message = "Email is required")
@Email(message = "Email must be a valid email address")
@Column(unique = true)
private String email;
@NotNull(message = "Password is required")
@Size(min = 8, message = "Password must be at least 8 characters")
@Pattern(regexp = "^(?=.*[A-Z])(?=.*[0-9]).+$",
message = "Password must contain at least one uppercase letter and one digit")
private String password;
@Min(value = 18, message = "Age must be at least 18")
@Max(value = 120, message = "Age must be at most 120")
private Integer age;
@DecimalMin(value = "0.0", inclusive = false, message = "Salary must be positive")
@DecimalMax(value = "999999.99", message = "Salary exceeds maximum")
@Digits(integer = 6, fraction = 2, message = "Invalid salary format")
private Double salary;
@NotNull
@Positive(message = "Score must be positive")
private Integer score;
@Past(message = "Birth date must be in the past")
private java.time.LocalDate birthDate;
@Future(message = "Expiry date must be in the future")
private java.time.LocalDate expiryDate;
@AssertTrue(message = "Terms must be accepted")
private boolean termsAccepted;
// getters/setters...
}
package com.example.validation;
import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.*;
// Step 1: Define the annotation
@Documented
@Constraint(validatedBy = UsernameValidator.class) // Link to validator class
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface ValidUsername {
String message() default "Username must be 3-20 chars, alphanumeric and underscores only";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
// Usage on entity field:
// @ValidUsername
// private String username;
package com.example.validation;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
// Step 2: Implement the validator logic
public class UsernameValidator
implements ConstraintValidator<ValidUsername, String> {
private static final String USERNAME_PATTERN = "^[a-zA-Z0-9_]{3,20}$";
@Override
public void initialize(ValidUsername annotation) {
// Called once before validation - can read annotation attributes here
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
if (value == null) return true; // Let @NotNull handle null check
if (!value.matches(USERNAME_PATTERN)) {
// Optionally customize the error message
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate(
"Username '" + value + "' is invalid. Use 3-20 alphanumeric chars.")
.addConstraintViolation();
return false;
}
return true;
}
}
package com.example.controller;
import com.example.model.User;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.validation.FieldError;
import java.util.*;
@RestController
@RequestMapping("/api/users")
public class UserController {
// @Valid triggers Bean Validation on the @RequestBody
// If validation fails, Spring throws MethodArgumentNotValidException
@PostMapping
public ResponseEntity<User> createUser(@Valid @RequestBody User user) {
// If we reach here, validation passed
return ResponseEntity.status(201).body(userService.save(user));
}
// Global exception handler for validation errors
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Map<String, String>> handleValidationErrors(
MethodArgumentNotValidException ex) {
Map<String, String> errors = new LinkedHashMap<>();
// Collect all field errors
ex.getBindingResult().getAllErrors().forEach(error -> {
String field = ((FieldError) error).getField();
String message = error.getDefaultMessage();
errors.put(field, message);
});
return ResponseEntity.badRequest().body(errors);
}
}
// Example error response for invalid input:
// {
// "name": "Name must be between 2 and 50 characters",
// "email": "Email must be a valid email address",
// "age": "Age must be at least 18"
// }
Hibernate should be learned as a practical Hibernate skill, not only as a definition. Start by asking what problem the topic solves, what input or state it receives, what rule it applies, and what visible result proves it worked.
A strong explanation of Hibernate includes the normal case, a boundary case, and a failure case. When you practice, write down the before-state, the operation, the after-state, and the reason the result changed.
This lesson was expanded because the audit reported: under 650 content words; limited checklist/practice/mistake/FAQ notes . The added notes below focus on clearer explanation, more examples, and concrete practice so the topic is easier to understand from the page itself.
Imagine you are adding Hibernate to a small learning project. The first step is to choose the smallest scenario that still shows the main idea. Avoid starting with a large production design; it hides the concept behind too many details.
Next, isolate the moving parts. Name the input, the rule, the output, and the possible error. This habit makes the topic easier to debug because you can see whether the problem is caused by bad data, wrong configuration, incorrect syntax, timing, permissions, or misunderstanding of the rule.
Finally, compare two versions: one correct version and one intentionally broken version. The broken version is valuable because it teaches you how the topic fails in real work, which is usually what interviews and debugging tasks test.
@Entity
@Table(name = "lesson_hibernate")
public class HibernateNote {
@Id
private Long id;
private String status;
public void markReviewed() {
this.status = "REVIEWED";
}
}
try (Session session = sessionFactory.openSession()) {
Transaction tx = session.beginTransaction();
HibernateNote note = session.find(HibernateNote.class, 1L);
note.markReviewed();
tx.commit();
}
// The important idea is to know when Hibernate tracks the object and when SQL is flushed.
Memorizing Hibernate as a definition only.
Pair the definition with a small working example and a failure example.
Copying syntax without checking the state before and after.
Write the input state, apply the rule, then inspect the output state.
Ignoring the error path for Hibernate.
Create one intentionally broken version and document the symptom and fix.
Memorizing Hibernate Validation @NotNull @Size @Email without the situation where it is useful.
Connect Hibernate Validation @NotNull @Size @Email to a concrete Hibernate task.
Understand the problem it solves, the input or state it works on, and the visible result that proves the concept is working.
Use one tiny correct example, one boundary example, and one broken example. Compare the output or state after each change.
They often memorize the term without tracing the behavior. Tracing makes the rule easier to remember and debug.
Remember the problem it solves in Hibernate, then attach the syntax or steps to that problem.
Explore 500+ free tutorials across 20+ languages and frameworks.