@Entity Annotation: It is used to indicate that a Java class is an entity that may be saved to a database. In a relational database, an entity class represents a table, and every instance of the entity class corresponds to a row in the table. The class declaration has to have the @Entity annotation.
@Table Annotation: It is used to indicate that instances of the class should be kept in a database table with the name "employee." It ought to be added to the entity class declaration. Example: @Table(name = "employee")
@Id: It is used to provide an entity's main key. Each row in the table must be uniquely identified by the main key for each entity class. A field may include the @Id annotation.
@GeneratedValue: To indicate how the primary key should be produced, use this annotation together with @Id. GenType.IDENTITY is a common tactic for auto-incremented columns.
Example: @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column: It is used to specify the attributes of a column in the database table. It can be used to customize the database column by specifying attributes such as the name, length, nullable, unique, and precision of the column.
Example: @Column(name = "first_name", length = 50, nullable = false)
@Transient: It's used to indicate that a field shouldn't be saved to the database. This indicates that a field or property will not be used and won't be saved in the entity's database table.
@Temporal: It is used to specify the type of temporal data (date, time, or timestamp) that a field representing a date or timestamp should be treated as.
Example: @Temporal(TemporalType.DATE)
@OneToOne: When two entities have a one-to-one relationship, this means that each instance of one entity is linked to exactly one instance of the other, and vice versa.
@ManyToOne: This specifies a many-to-one relationship between two entities. It is used to map a foreign key column from a child table to a parent table's primary key column.
@OneToMany: This specifies a one-to-many relationship between two entities. A group of child entities are mapped to a single parent entity using it.
Example: @OneToMany(mappedBy = "author")
@ManyToMany: In Hibernate, you normally annotate a field in one of the entities with the @ManyToMany annotation to specify a many-to-many relationship. This field displays a grouping of similar elements.
@JoinColumn: This specifies the database table column that joins two entities in a relationship. Example: @JoinColumn(name = "author_id")
@JoinTable: This annotation permits the adjustment of attributes related to the intermediate table utilized in a many-to-many association. It offers the capability to define the table's name and the columns serving as foreign keys.
Example: @JoinTable(name = "student_course", joinColumns = @JoinColumn(name = "student_id"), inverseJoinColumns = @JoinColumn(name = "course_id"))
Cascade Type
Cascade Types In Hibernate, cascade types are used to specify how changes to an entity's state should be propagated to its linked entities.
In other words, when you update one entity, such as by storing or deleting it, it might also be necessary to change any associated entities. The connected entities that should be impacted and how are specified by cascade kinds.
Hibernate offers the following cascades:
CascadeType.ALL: Applies all cascade types, including PERSIST, MERGE, REMOVE, REFRESH, and DETACH, to the linked entities.
CascadeType.PERSIST: Saves the state of connected entities when the owner entity is saved.
CascadeType.MERGE: When the owner entity is merged, it updates the state of the associated entities.
CascadeType.REMOVE: Removes the connected entities when the owner entity is deleted.
CascadeType.REFRESH: Refreshes the state of connected entities when the owner entity is refreshed.
CascadeType.DETACH: Detaches the connected entities when the owner entity is detached.
Comentários