Quite often there is a need to apply validation to some of the bean's properties based on the state of the other properties of the same bean. Bean Validation allows to do this in several ways: usage of @AssertTrue on a getter method with validation logic usage of @ScriptAssert on a class level (Hibernate Validator specific) usage of custom constraint annotation on a class level To show each of the approaches let us consider two classes representing a library member and a book
As it was discussed in one of the previous posts JPA: Mapping Enums the right way, JPA 2.1 standardized AttributeConverters, which can, for example, be used to map Java enums in more controlled/predicted way. Let's quickly recap how it can be done. Using attribute converter to map java enum Let's assume we have a PhoneType enum and a simple entity that has this enum as a field. This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
Stream API received a few nice additions with the Java 9. Both Stream interfaces as well as Collectors were updated. Let's start with the new collectors. To show these improvements we will use next classes: This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters public class Student { private String firstName; private String
In this post we will take a look at couple of ways how many-to-many relationships, which have additional information, can be mapped to entities using JPA. @ManyToMany annotation cannot be applied in such cases as it does not allow to map additional information from the join table. To show these approaches let's consider such small film rental database schema: We have three main entity tables - actor, film, and customer. Where film_to_actor as well as rental can be called join tables with additional information. Actors

Java 9 brings a lot of big new features, tools and API enhancements. But somehow the most excitement comes from a few simple additions to collections at which we will look today. JEP 269: Convenience Factory Methods for Collections So why so many people are so excited about JEP 269? Creating a collection from a number of elements is actually an operation very frequently used in the code. How many times did you wrote something like: This file contains bidirectional Unicode text that
In the next post of "Code generator" series we will will talk about Immutables. This annotation processor based code generator allows you to generate simple, safe and consistent value objects, without the need to write Builders, factory methods and so on... It is easy to use, it allows customization, and all you need to do is to define your model value object as interface or abstract class. To start using Immutables, all you need to do is to add it to your dependencies. For example

By this post I'll be starting a series of posts dedicated to different code generators, which makes our lives easier, and more fun. As we don't need to write boring code! Did you ever find yourself in a situation where you needed to copy data from one Java bean to another? Or maybe from an Entity to DTO? Does next code look familiar to you ? This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To
And the mapping is...