3. Hibernate Annotation

Hibernate Annotation 


- Hibernate is a popular Java-based framework for working with relational databases. 
- It simplifies the process of database interactions by providing an object-relational mapping (ORM) solution. 
- Annotations in Hibernate are metadata provided in the form of Java annotations that help configure and map Java objects to database tables. 
- These annotations are used to define the mapping between Java classes and database tables, specify primary keys, relationships, and other aspects of the object-relational mapping.


Some commonly used Hibernate annotations:

1. @Entity:
    - Used for declaring any POJO class as an entity for a database.
   - Marks a Java class as an entity, indicating that instances of this class will be persisted to the database. Each entity class typically corresponds to a table in the database.
   - it tells Hibernate that this class represents a table in the database.

    @Entity
    public class Employee {
        // class definition
    }
    
2. @Table:
    - This annotation specifies the details of the database table that corresponds to the entity.
   - Used to change table name, schema, and other properties.

    @Entity
    @Table(name = "employees", schema = "public")
    public class Employee {
        // class definition
    }



@Column: This annotation allows you to customize the mapping of a field to a database column. You can specify the column name, type, length, and other properties.


3. @Id:
    - This annotation marks a field as the primary key of the entity. 
    It indicates which field uniquely identifies each record in the database table.
    
   @Id
    private int id;
    

4. @GeneratedValue:
    - This annotation specifies the strategy for generating values for the primary key field. 
    - It can be used to automatically generate unique primary key values, such as using an auto-increment column in the database.

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Long id;
    
    
5. @Column:
    - Allows you to customize (changes) the mapping of the annotated field to the database column. 
    - You can specify the column name, length, nullable, etc.

    
    @Column(name = "employee_name", nullable = false, length = 50)
    private String name;

6. @Transient - This Tells  to hibernate, not to add this particular column in table.
    
7. @Temporal - This annotation is used to format the date for storing in the database.

8. @Lob : Used to tell hibernate that it’s a large object and is not a simple object

9. @OrderBy : This annotation will tell hibernate to OrderBy as we do in SQL.

            For example – we need to order by student firstname in ascending order

            @OrderBy(“firstname asc”)


Hibernate annotations provide a powerful and flexible way to map Java objects to database tables and define relationships between entities.

Comments

Popular posts from this blog

5. JPA Relational Mapping & Fetch Types & Cascadding

4. Data Fetching Methods & @Embeddable annotation in Hibernate

6. Hibernate Query Language (HQL) & Caching in Hibernate