INTRODUCTION

We are covering Hibernate annotations and Lombok annotations. How different annotations are helped to solve the XML mapping file. Hibernate Annotations is the powerful way to provide the metadata for the Object and Relational Table mapping. Lombok Annotations are useful for developer to save time and remove boilerplate code, you can avoid writing getters, setters, constructors, toString, equals, hash Code, and other common methods. Below are some annotations and their uses explained.


Hibernate Annotations

1) @Id  : @Id annotation indicate the primary key in the entity class. The primary key uniquely identifies each row or record in the table and is used for efficient retrieval and identification of specific entities. The @Id annotation will automatically determine the most appropriate primary key generation strategy to be used but you can override this by applying the @GeneratedValue annotation, which takes two parameters strategy and generator

Example : 
  
@Entity  
@Table(name = "student")              
 public class Student {

  @Id
  @GeneratedValue(strategy  = GenerationType. AUTO)
  private long rollNo;

}
                

2) @Entity : @Entity is used in java based ORM (Object Relational Mapping) framework. It marks the class as @Entity class so that Instance of class will be mapped with database table. When you mark @Entity annotation to a class , Each class correspond to a database table and class fields are mapped with a column name. It is present in javax.persistence package

Example : 

@Entity  
@Table(name = "student")              
 public class Student {

  @Id
  @GeneratedValue(strategy  = GenerationType. AUTO)
  private long rollNo;

}

3) @Table : @Table annotation used for adding table name of particular MYSQL database. It provide the flexibility in naming the table. It contain parameter name and followed with table name. It also provide other optional attributes like schema , catalog , indexes.

Example :  

@Entity  
@Table(name = "student")              
 public class Student {

  @Id
  @GeneratedValue(strategy  = GenerationType. AUTO)
  private long rollNo;

}

4) @GeneratedValue : @GeneratedValue annotation is used with @Id annotation as if you want to generated the value of primary key in table automatically. There are different methods to generate primary key values like AUTO, IDENTITY, SEQUENCE and TABLE.

Example : 

@Entity  
@Table(name = "student")              
 public class Student {

  @Id
  @GeneratedValue(strategy  = GenerationType.AUTO/IDENTITY/TABLE/SEQUENCE)
  private long rollNo;

}

5) @Query : @Query annotation is used to define the custom queries that can be executed on the database, it basically gives you SQL queries to fetch or update the data. @Query annotation is used at DOA layer or Repository interface where we are fetching or manipulating data.

Example : 

@Repository
public interface Student extends JpaRespository<Student, Long> {

     @Query("select s from Student s where s.division = division")
     List<Student> fetchAllData(@param("division",division));
   
}

6) @Column : @Column annotation used to provide flexibility in customizing the column's attributes and behavior. It is added on field level of entity class. It provides the attributes like column name, data type, length, and nullable. You have more control over the mapping between Java class fields and the corresponding database columns.

Example : 

@Entity  
@Table(name = "student")              
 public class Student {

  @Id
  @GeneratedValue(strategy  = GenerationType.AUTO/IDENTITY/TABLE/SEQUENCE)
  private long rollNo;

  @Column(name = ''stud_name", length = 100)
  private String name;

}

7) @Transient : @Transient annotation is used with ORM framework like JPA and Hibernate.
@Transient framework is used on field of entity class which should be ignored and not stored in database. If large object or data we need to process but not wanted to store in database we can use @Transient annotation on that field or object.

Example : 

@Entity  
@Table(name = "student")              
 public class Student {

  @Id
  private long rollNo;

  private String userName;

   @Transient
   private String password;

}

Lombok Annotations


1) @Data : @Data annotation is equivalent to the combination of following annotation like @Getter ,
@Setter , @ToString, @EqualsAndHashcode, @RequiredArgsConstructor. So use of @Data annotation helps developer time in writing getter , setter method , constructor and toString methods.

Example :   

@Data          
 public class Student {

  @Id
  private long rollNo;
}


2) @Builder : @Builder pattern used to create builder pattern automatically. @Builder pattern also also avoid writing of boilerplate code. 

Example : 

@Builder       
 public class Student {

  @Id
  private long rollNo;

  private String name;
}

3) @EqualsAndHashCode : It is provided by Lombok library. @EqualsAndHashCode used to generate equals() and hashcode() methods automatically. It's important to note that when using @EqualsAndHashCode, all non-static, non-transient fields are considered for equality and hash code generation by default.

Example : 

@EqualsAndHashCode     
 public class Student {

  @Id
  private long rollNo;

  private String name;
}

4) @NoArgsConstructor : @NoArgsConstructor is provided by Lombok library which is  used to create the no-argument constructor. we don't need to add the constructor explicitly, Annotation will provide the constructor automatically.

Example : 


@NoArgsConstructor   
 public class Student {

  @Id
  private long rollNo;

  private String name;

}

5) @AllArgsConstructor :  @AllArgsConstructor is provided by Lombok library which is used to create the constructor with all field present in the class. It used to initialize all the fields in the class.


Example : 

@AllArgsConstructor
 public class Student {

  @Id
  private long rollNo;

  private String name;

}


Below are some additional annotations used as per requirement.

1) @Size
2) @Document
3) @Transactional
4) @mapping


USEFUL LINKS : 

Spring boot annotations : 


RedisVs Hazelcast :


GIT commands : 

About us

I am Suraj Ladda and working as Software Developer in IT industry.

Contact us

surajladda07@gmail.com