Basic Hibernate Annotations

Reading Time: 2 minutes

If you are reading this blog I assume you are already familiar with the Hibernate basics. If not, please visit “Introduction to Hibernate” to get started with Hibernate.

Hibernate annotations may be considered as metadata to link Java applications to the relational database. Also used to map the traditional relational databases with frontend Java applications. It works as a bridge to connect Java applications to relational database either with the help of a file ending with “.hbm” containing all the necessary mapping or the same mapping can be done using annotations. Annotations can be identified by “@” at the start of the entity for example: @Entity, @table, @Id, etc.

@Entity

  • Package: javax.persistence.Entity
  • Every persistent POJO class is an entity and it is declared using the @Entity annotation.
  • Used with model classes to specify that they are entity beans.
@Entity
public class Employee implements Serializable {
…
}

@Table

  • Package: javax.persistence.Table
  • Used with entity beans which is used to define the corresponding table name in the database.
  • Specifies the database table, this Entity maps to using the name attribute of @Table annotation.
@Entity
@Table(name = "employee")
public class Employee implements Serializable {
...
}

@Column

  • Package: javax.persistence.Column
  • Used to define the column name in a database table.
  • Specifies the column mapping using @Column annotation.
@Entity
@Table(name = "employee")
public class Employee implements Serializable {
  @Column(name = "name")
  private String name;
...
}

@Id

  • Package: javax.persistence.Id
  • Used to define the primary key in the entity bean.
  • Annotate the id column using @Id.
@Entity
@Table(name = "employee")
public class Employee implements Serializable {
  @Id
  @Column(name = "id")
  private int id;
...
}

@GeneratedValue

  • Package: javax.persistence.GeneratedValue
  • Used to define the strategy to be used for generation of primary key.
  • Used in conjunction with javax.persistence.Generation Type enum eg : GenerationType.IDENTITY.
  • Let database generate (auto-increment) the id column.
@Entity
@Table(name = "employee")
public class Employee implements Serializable {
  @Id
  @Column(name = "id")
  @GeneratedValue
  private int id;
...
}

@Version

  • Package: javax.persistence.Version
  • Control versioning or concurrency using @Version annotation.
@Entity
@Table(name = "employee")
public class Employee implements Serializable {
  @Version
  @Column(name = "version")
  private Date version;
...
}

@OrderBy

  • Package: javax.persistence.OrderBy
  • Sort your data using @OrderBy annotation.
  • asc used for sorting in ascending order while desc is used for sorting in descending order
  • In the example below, it will sort all contacts in a company by their Age in ascending order.
 @OrderBy("Age asc")
  private Set contacts;

I hope you are now able to understand the Basic Hibernate Annotations. Stay tuned for the next part related to Hibernate Association and Inheritance Mapping Annotations and more.

To know more about Hibernate, you can visit this link.

knoldus-blog-footer-banner.jpg?

Written by 

Kuldeep is a Software Consultant at Knoldus Software LLP. He has a sound knowledge of various programming languages like C, C++, Java, MySQL, and various frameworks like Apache Kafka and Spring/Springboot. He is passionate about daily and continuous improvement.

Discover more from Knoldus Blogs

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

Continue reading