13

My Java SE Technology Stack - Hibernate Validator

 3 years ago
source link: https://www.linkedin.com/pulse/my-java-se-technology-stack-hibernate-validator-padr%C3%B3n-urdaneta/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
by Georgia Simpson

My Java SE Technology Stack - Hibernate Validator

Published on M 15, 2020M 15, 2020 • 1 Likes • 0 Comments

Edinson E. Padrón Urdaneta

Edinson E. Padrón UrdanetaFollow

Test Automation Engineer at Globant

  • 分享
    0

The story

In 2014, I took Udacity's Software Debugging course (CS259). The lessons are so packed with information that over the years I have revisited my notes over and over again just to make sure I'm not missing something on such a fundamental subject. Among all the topics covered by Mr. Andreas Zeller, preconditions, postconditions, and invariants were the ones that continue to amaze me even today. These concepts are an essential part of the design by contract approach and this is how Zeller describes them:

  • A precondition is a group of assertions being called at the beginning of a function and which checks the properties of the arguments.
  • A postcondition is a group of assertions being called at the end of a function and which checks the result of it.
  • An invariant is a condition that always (at the beginning and at the end of each public method) hold for a data object.

While I agree they can behave as assertions, I prefer the term constraints, since we can decide not to throw an exception when one of them is not fulfilled (as assertions usually tend to do), and rather log the violation, for example.


Most of us may be familiarized with the @NotNull annotation, the simplest specimen we can find in the wild, which at least documents that a method's argument shouldn't be null, or that the caller of a method shouldn't get null as a value returned by it, depending on what is annotated: a method's parameter or the method itself. @NotNull is just a tiny piece of the Jakarta Bean Validation (JBV) specification, with which we can express preconditions, postconditions, and invariants.

But an specification is not enough, it just an API, a skeleton. We need the muscles for actually performing the validations and find the violations (if any) present in the system for the constraints we have defined on its domain objects.

The tool

Hibernate Validator is the reference implementation of the Bean Validation specification and it's part of the Hibernate umbrella (the ORM being the most known member). If you have used Spring + Hibernate, there's a good chance you have used Hibernate Validator without knowing it. But don't worry, we can make use of it outside JEE.

We are able to declare and validate constraints on fields, properties, container elements, method's parameters, return values, among others. And as a plus, all of these component's documentation gets enriched with the annotations used for describing their respective constraints, avoiding potential inconsistency that can be introduced by manually including such information. Additionally, Hibernate Validator extends the set of constraints defined by JBV with a fan of very convenient and useful ones, supports class-level constraints (which JBV lacks), and even provides a way to declare our own custom constraints.


If following the design by contract approach, while having a orthogonal, descriptive, simple, and powerful validation harness that not only ensures our APIs receive their input as expected, but also verifies we are delivering on our promise to our clients without doing either by ourselves is what we want, then Hibernate Validator is definitely what we need.


Are you using Hibernate Validator? Why or why not? What are you using for preconditions, postconditions, and invariants in your code? Let us know in the comment section below.

________________________________________________________________________

This article is part of a series. You can read the previous issues below.

Edinson E. Padrón Urdaneta

Edinson E. Padrón Urdaneta

Test Automation Engineer at Globant

关注

In this issue, we will talk about the design by contract approach and its core concepts: preconditions, postconditions, and invariants.

This is the seventh article in a series covering the Java technology stack I'm putting together in this quarantine.

#java #softwaredevelopment #softwaredesign #softwareengineering #programming #coding #debugging #hibernate #validator #preconditions #postconditions #invariants

0 comments
article-comment__guest-image
Sign in to leave your comment

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK