Tag: hibernate
-
What is @MapsId used for in JPA/Hibernate? – Part II
In the previous post , we saw how and why to use @MapsId in a One to One relationship. In this post , lets see how it comes handy in a One to Many relationship. Let’s take the same Musician entity as the parent object. Let’s take an Album entity as the child object. A…
-
What is @MapsId used for in JPA/hibernate? – Part 1
@MapsId annotation. If you ever wondered what this annotation is for , here is an in depth analysis of it: When is this annotation used? It is used if the primary key of a child table is the same as the primary key of a parent table Why is this used? By specifying the annotation…
-
How to group related attributes together while doing hibernate entity mapping?
Let’s say you have chosen Hibernate ORM to save data to your database. For each table you have written a corresponding entity class in your java application. And then while doing so you start noticing that a lot of the entity classes have few fields repeated. You wonder if it would be nice to keep…
-
What are the different ways to delete a child entity in JPA/hibernate through Spring Data?
Let’s say you have an application which creates blog posts and saves them and also the comments user enter on the posts. The Blogpost and the comment represent a parent and child relationship. You may want to delete the comments in three different ways: Delete the comment whenever the blogpost is deleted. Delete the comment…
-
How to automatically update creation and update timestamp through hibernate?
Let’s say you have two columns in all of your tables , one to track when a record was created and another to track when it was last updated. You have chosen hibernate as your ORM framework and created corresponding entity classes as well. Hibernate allows you to update these columns automatically ! You just…
-
How to auto generate and auto increment identifier values for PostGreSQL tables through Hibernate?
I was trying to connect to PostGreSQL database through Spring Boot. I used spring-boot-starter-data-jpa dependency which automatically included hibernate dependencies to the project. And since I was connecting to postgresql database I included the following postgresql dependency as well: I created a simple table named my_table with an integer ID and a JSON data type…