Can you list down important annotations used for Hibernate m

ghz 9months ago ⋅ 184 views

Certainly! Hibernate provides annotations that allow developers to define object-relational mappings directly in Java classes, simplifying the mapping process and reducing the need for XML mapping files. Here are some of the important annotations commonly used for Hibernate mapping:

  1. @Entity: Marks a class as an entity, representing a persistent object that can be stored in the database. The class should have a corresponding database table.

  2. @Table: Specifies the name of the database table to which the entity is mapped. It can also define additional table-level properties such as schema and catalog.

  3. @Id: Specifies the primary key attribute of the entity. It can be applied to a field or a getter method and indicates that the attribute is the primary identifier for the entity.

  4. @GeneratedValue: Specifies the strategy for generating primary key values automatically. It can be used in conjunction with @Id to specify how primary key values are generated (e.g., AUTO, IDENTITY, SEQUENCE).

  5. @Column: Maps an entity attribute to a database column. It allows you to specify various column properties such as name, type, length, nullable, unique, and column definition.

  6. @Basic: Specifies the default fetch type for an entity attribute. It can be used to configure the fetching behavior of an attribute (e.g., lazy or eager fetching).

  7. @OneToOne: Defines a one-to-one relationship between two entities. It indicates that an attribute in one entity references another entity.

  8. @OneToMany: Defines a one-to-many relationship between two entities. It indicates that an attribute in one entity references a collection of related entities.

  9. @ManyToOne: Defines a many-to-one relationship between two entities. It indicates that an attribute in one entity references another entity, and multiple entities in the referencing entity can reference the same entity.

  10. @ManyToMany: Defines a many-to-many relationship between two entities. It indicates that an attribute in one entity represents a collection of related entities, and each entity in the collection can be related to multiple entities in another entity.

  11. @JoinColumn: Specifies the join column used for mapping a relationship between entities. It allows you to customize the name, nullable, and referencedColumnName properties of the join column.

  12. @JoinTable: Specifies the join table used for mapping a many-to-many relationship between entities. It allows you to customize the name, joinColumns, and inverseJoinColumns properties of the join table.

These annotations provide a convenient way to define object-relational mappings directly in Java classes, making it easier to manage entity mappings and relationships in Hibernate-based applications.