Hibernate — JPA Annotations -1 (Entity Management)

Hibernate — JPA Annotations -1 (Entity Management)

Welcome to the first part of our series on Hibernate and JPA Annotations. In this installment, we will delve into the essential concept of “Entity Management.” Understanding how to effectively manage entities is crucial in the world of Java Persistence API (JPA) and Hibernate. As we explore the intricacies of entity management, we will uncover the power of annotations in shaping the behavior of our entities. Whether you are a seasoned developer or a newcomer to Hibernate, this series aims to provide valuable insights into the world of Java-based persistence. Let’s embark on this journey to master the art of entity management with Hibernate and JPA Annotations.

I am going to explain the topic using a real project, so I create a Maven project using start.spring.io and download it.

I add the following settings to the application.properties file. (I assume you have PostgreSQL installed on your PC, or feel free to use your favorite database.):

# PostgreSQL Configuration
spring.datasource.url=jdbc:postgresql://localhost:5432/<your postgresql db name here>
spring.datasource.username=<your postgresql user name here>
spring.datasource.password=<your postgresql password here>
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.hibernate.ddl-auto=update        

And I create the Employee class and add some annotations to it:

package com.azizkale.anotations_tutoraial.api;
import jakarta.persistence.*;

@Entity
@Table(name="employee")
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column
    private int id;
    @Column
    private String name;
}        

Now, when you run the project, you will observe that Hibernate has created a table for the Employeeentity in your database. Not only does it stop there, but it has also added some features to the table.

Let’s discuss these features and the meanings of annotations:

@Entity

The @Entity annotation indicates that a Java class is marked as a Java Persistence API (JPA) entity. JPA provides a standard for object-relational mapping (ORM) in Java applications, and this annotation specifies that a class will be mapped to a database table.

The @Entity annotation provides the following basic elements:

  • Table Naming: If the class name differs from the table name, the name attribute can be used to specify the table name.
  • Database Schema: If a database schema needs to be specified, it can be done using the schema attribute.
  • Catalog Name: If a catalog name needs to be specified, it can be done using the catalog attribute.
  • Indexing: Indexes can be created in the database using indexing properties.more: https://medium.com/@azizkale/hibernate-jpa-annotations-1-entity-management-91a6d9357cad

要查看或添加评论,请登录

社区洞察

其他会员也浏览了