filmov
tv
Hibernate 6 EmbeddableInstantiator – Instantiate embeddables your way

Показать описание
Embeddables are simple Java objects. They provide an easy way to define and group a set of attributes that become part of your entity. Developers often use them to create reusable mapping information and handle them using the same piece of business code.
Unfortunately, the JPA specification and Hibernate until version 6.0.0, required your embeddable to have a default constructor. This might be OK if you’re fetching an entity from the database. Hibernate then automatically sets all attributes before providing the object to your business code. But a default constructor is not always a great idea. E.g., if some of the attributes are mandatory and you instantiate embeddables in your business code or frontend. In that case, a constructor that sets all mandatory attributes would be a much better fit.
Since Hibernate 6.0.0, you can easily define how Hibernate instantiates and initializes your embeddable. You can use that, for example, to remove the requirement of a default constructor. I will show you how to do that in this article.
Like my channel? Subscribe!
Want to connect with me?
Unfortunately, the JPA specification and Hibernate until version 6.0.0, required your embeddable to have a default constructor. This might be OK if you’re fetching an entity from the database. Hibernate then automatically sets all attributes before providing the object to your business code. But a default constructor is not always a great idea. E.g., if some of the attributes are mandatory and you instantiate embeddables in your business code or frontend. In that case, a constructor that sets all mandatory attributes would be a much better fit.
Since Hibernate 6.0.0, you can easily define how Hibernate instantiates and initializes your embeddable. You can use that, for example, to remove the requirement of a default constructor. I will show you how to do that in this article.
Like my channel? Subscribe!
Want to connect with me?