|
Answer» Hibernate framework provides support to JPA annotations and other useful annotations in the org.hibernate.annotations package. Some of them are as follows: - javax.persistence.Entity: This annotation is used on the model classes by using “@Entity” and tells that the classes are entity beans.
- javax.persistence.Table: This annotation is used on the model classes by using “@Table” and tells that the class maps to the table name in the database.
- javax.persistence.Access: This is used as “@Access” and is used for defining the access type of EITHER field or PROPERTY. When nothing is specified, the default value taken is “field”.
- javax.persistence.Id: This is used as “@Id” and is used on the attribute in a class to indicate that attribute is the primary key in the bean entity.
- javax.persistence.EmbeddedId: Used as “@EmbeddedId” UPON the attribute and indicates it is a composite primary key of the bean entity.
- javax.persistence.Column: “@Column” is used for defining the column name in the database table.
- javax.persistence.GeneratedValue: “@GeneratedValue” is used for defining the strategy used for primary key generation. This annotation is used along with javax.persistence.GenerationType enum.
- javax.persistence.OneToOne: “@OneToOne” is used for defining the one-to-one mapping between two bean entities. Similarly, hibernate provides OneToMany, ManyToOne and ManyToMany annotations for defining different mapping types.
org.hibernate.annotations.CASCADE: “@Cascade” annotation is used for defining the cascading action between two bean entities. It is used with org.hibernate.annotations.CascadeType enum to define the type of cascading. Following is a sample class where we have used the above listed annotations: package com.dev.interviewbit.model;import javax.persistence.Access;import javax.persistence.AccessType;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.OneToOne;import javax.persistence.Table;import org.hibernate.annotations.Cascade;@Entity@Table(name = "InterviewBitEmployee")@Access(value=AccessType.FIELD)public class InterviewBitEmployee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "employee_id") private long id; @Column(name = "full_name") private STRING fullName; @Column(name = "email") private String email; @OneToOne(mappedBy = "employee") @Cascade(value = org.hibernate.annotations.CascadeType.ALL) private Address address; //getters and setters methods}
|