Base Entity and Audit entity with JPA

Abdalrhmanalkraien
CodeX
Published in
3 min readMar 18, 2023

Introduction:

When creating any web application and wanting to connect it with DB, we will add id a column inside each class and createionDate updatedDate in all entity classes, and it is not a clean code. So in this article, I will cover how can we avoid these practices and write clean code when creating our entities.

Setup the project:

When creating a new project to practice it, we need to use the spring boot framework and, we will use MAVEN to manage our dependency.

<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.9.Final</version>
</dependency>
</dependencies>

After preparing our Pom.xml file and put the major dependency, We need to create a new Class called BasedEntity.class , inside It, we will put inside it the major filed inside the database like id and other common filed inside your application.

@Getter
@Setter
@MappedSuperclass
public abstract class BaseEntity implements Serializable {

@Id
@GeneratedValue
@Column(columnDefinition = "BINARY(16)", updatable = false, nullable = false)
private UUID id;
}

I think you have some ambiguous points about the above class I will explain each line:

  • MappedSuperclass : is used to ensure that the BaseEntity class will not have a separate representation as the table of the extending class.
  • public abstract class BaseEntity : to prevent developers from instantiating an instance of this class. Only extending classes can instantiate an instance.
  • @getter and @setter : to generate getter and setter methods with losly code.

And now I will create a new class to represent the Audit class, And I will define createdAt and updatedAt.

@Getter
@Setter
@MappedSuperclass
public abstract class BaseEntityAudit extends BaseEntity implements Serializable {
private String createdBy;
private String updatedBy;

@CreationTimestamp
@Column(name = "created_at", updatable = false)
private Date createdAt;

@UpdateTimestamp
@Column(name = "updated_at")
private Date updatedAt;
}

So in the above Class, I extend the class from BaseEntity class. And I defined two fields to represent audit data.

So I will create a new entity class to define and use the above classes with it.

@Entity
@Getter
@Setter
@Table(name = "users")
public class User extends BaseEntityAudit {

@Column(name = "name")
private String name;

@Column(name = "email")
private String email;
}

And now like what you saw, I create a new class called User and the class extend data from BaseEntityAudit , so the User class will have 5 fields in the database id , createdAt , updatedAt , name , and email . And if we need to create another class we will avoid defining id and createAt in each entity class.

Conclusion:

I think the above approach is a good approach to creating an entity class with readable code, is not redundant, and you will not define the same field more than one time.

Finally, don’t forget to share and clap the articles and you can clap 50 times on each article. and you find my LinkedIn here.

--

--

Abdalrhmanalkraien
CodeX
Writer for

I am a Java Developer and DevOps Engineer. I used the latest Technology for build completed development or Deployment