Understand Hibernate @ManyToOne, @OneToMany With Examples

Understand Hibernate @ManyToOne, @OneToMany

Overview @ManyToOne and @OneToMany are standard mappings in Hibernate. Understanding how to use them correctly is the key to mapping entities efficiently in Hibernate. In this post, I will show you how to use these annotations and give examples to demonstrate use cases. The analogy I use is schools and students. In real life, schools … Read more

Using Custom Converter In Hibernate

Using Custom Converter In Hibernate

Overview Imagine this scenario, you have a legacy database that store weather data. The previous developer, due to various reason, store the temperature in the following format: id location temperature capture_date 1 Hanoi 30.50 C 2023-07-31 22:10:30.000 2 New York 95.20 F 2023-07-31 22:10:30.000 As you can see, the temperature stored in a sub-optimal way … Read more

Using @ColumnTransformer In Hibernate

Using @ColumnTransformer In Hibernate

Overview One of the lesser-known yet powerful annotations in Hibernate is @ColumnTransformer. This annotation let us apply SQL transformations on entity properties when interacting with the database. You can think of this annotation as a two way converter. A real life use case Let’s consider a weather app that initially use Kelvin scale to store … Read more

How To use @Formula in Hibernate

How To use @Formula in Hibernate

Overview From the official documentation, a Formula mapping defines a “derived” attribute (that means, not persisted), whose state is determined from other columns and functions when an entity is read from the database. You’d probably know other annotation that make a field not persisted to the database: @Transient. While they both allow you to create … Read more