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 them in a separate class and then inherit it in all your entity classes.

That is possible!

You just need to annotate that parent class with @MappedSuperClass annotation and then start reusing it across any number of entities.

Hibernate will consider that class as an application level inheritance and will not create corresponding table in the database.

Let’s look at this with an example .

Let’s say you have two entity classes , Student.java and Employee.java:

@Entity
public class Student{


    @Id
	private Long id;

	private String name;

    private String className;

    private String schoolName;
	
    private LocalDate createdDate;

	private String createdBy;

	private LocalDate updatedDate;
	
	private String updatedBy;


}
@Entity
public class Employee{

    @Id
	private Long id;

	private String name;

    private String companyName;

    private String role;

	private LocalDate createdDate;

	private String createdBy;

	private LocalDate updatedDate;
	
	private String updatedBy;

}

(Getters, Setters , Equals and HashCode methods are removed for simplicity)

Both of them are annotated with @Entity annotation. So hibernate will create a student table and an employee table with the above attributes automatically if you set hibernate.hbm2ddl.auto flag to true.

If you see the above two entities a lot of attributes are common.

We can move these to a separate class :

package com.example.demo;

import java.time.LocalDate;

import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

@MappedSuperclass
public class BaseEntity {

	@Id
	private Long id;

	private String name;

	private LocalDate createdDate;

	private String createdBy;

	private LocalDate updatedDate;
	
	private String updatedBy;

	
	
	
}

Notice the annotation @MappedSuperClass and the absence of the annotation @Entity

Now you can refactor your Student and Employee entity classes like this:

package com.example.demo;

import javax.persistence.Entity;

@Entity
public class Student extends BaseEntity {

	private String className;

	private String schoolName;

	

}

package com.example.demo;

import javax.persistence.Entity;

@Entity
public class Employee extends BaseEntity{

	
	private String companyName;
	
	private String role;

	
	
	
}

Much simpler!

If you are going to add more entities which need the same fields you can inherit BaseEntity class in those classes.

I tried this in Spring Boot using in memory H2 database and it got created as expected:

That’s it!


Posted

in

,

by

Comments

Leave a Reply

Discover more from The Full Stack Developer

Subscribe now to keep reading and get access to the full archive.

Continue reading