0% found this document useful (0 votes)
72 views

Annotation

The document discusses various Hibernate annotations used for mapping Java classes to database tables. It describes annotations for entity mapping like @Entity, @Table, @Id, @GeneratedValue, @Column, @Temporal, @EmbeddedId and @IdClass. It also covers annotations for association and collection mapping like @OneToOne, @OneToMany, @ManyToOne, @ManyToMany, @JoinTable and @JoinColumn. Examples are provided for generating primary keys, specifying column details, mapping date fields and defining composite primary keys.

Uploaded by

Amit Jain
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Annotation

The document discusses various Hibernate annotations used for mapping Java classes to database tables. It describes annotations for entity mapping like @Entity, @Table, @Id, @GeneratedValue, @Column, @Temporal, @EmbeddedId and @IdClass. It also covers annotations for association and collection mapping like @OneToOne, @OneToMany, @ManyToOne, @ManyToMany, @JoinTable and @JoinColumn. Examples are provided for generating primary keys, specifying column details, mapping date fields and defining composite primary keys.

Uploaded by

Amit Jain
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Annotation HIBERNATE

ProDev IT Solutions, Indore (M.P.) [email protected] 09425431512


1

Hibernate Annotation
@Entity @Table @Id @GeneratedValue @GenericGenerator @Column @UniqueConstraint @Temporal @Transient @Embeddable @EmbeddedId @IdClass

ProDev IT Solutions, Indore. | Amit Bhandari -9425431512

Mapping
Collection
@CollectionOfElements @IndexColumn @CollectionId @MapKey @OneToOne @OneToMany @ManyToOne @ManyToMany

Association

@JoinTable @JoinColumn

ProDev IT Solutions, Indore. | Amit Bhandari -9425431512

@JoinTable
name joinColumn inverseJoinColumn

ProDev IT Solutions, Indore. | Amit Bhandari -9425431512

@Table
@Table name=A String value uniqueConstraints=Array of @UniqueConatraint @UniqueConatraint columnName=A string value or String[] Example:
@Entity @Table(name="business_clients",uniqueConstraints={ @UniqueConstraint(columnNames={"name","city"}), @UniqueConstraint(columnNames="email")} ) public class BusinessClient {
}

ProDev IT Solutions, Indore. | Amit Bhandari -9425431512

@GeneratedValue
@GeneratedValue
strategy=GenerationType.ConstantName generator=generator name defined by GenericGenerator

@GenericGenerator
name=user defined name to strategy like incr strategy=hibernate strategy like increment

GenerationType Constants
AUTO IDENTITY TABLE SEQUENCE

ProDev IT Solutions, Indore. | Amit Bhandari -9425431512

Example1
@Entity @Table(name="course_db") @GenericGenerator(name="incr",strategy="increment") public class Course { private int cid; @Id @GeneratedValue(generator="incr") public int getCid() { return cid; }
ProDev IT Solutions, Indore. | Amit Bhandari -9425431512 7

Example2
@Entity @Table(name="course_db") public class Course { private int cid; @Id @GeneratedValue(strategy=GenerationType.AUTO) public int getCid() { return cid; }
ProDev IT Solutions, Indore. | Amit Bhandari -9425431512 8

@Column
@Column
name length nullable unique precision scale

ProDev IT Solutions, Indore. | Amit Bhandari -9425431512

@Temporal for Date type


@Temporal
value=constant value from TemporalType

@TemporalType
DATE TIME TIMESTAMP

Example
@Temporal(value=TemporalType. TIMESTAMP) Date getBillDate(){ return billDate; }
ProDev IT Solutions, Indore. | Amit Bhandari -9425431512 10

EmbeddedId
StudentId class for Composite ID
public class StudentId implements Serializable{ private int rollNo; private String sem; . }

Student class
@Entity @Table(name="student_table") public class Student { private StudentId sid; private String name; private String course; @EmbeddedId public StudentId getSid() { return sid; } .. }
ProDev IT Solutions, Indore. | Amit Bhandari -9425431512 11

IdClass
StudentId class for Composite ID public class StudentId implements Serializable{
private int rollNo; private String sem; . }

Student class
@Entity @Table @IdClass(StudentId.class) public class Student{ private int rollNo; private String sem double marks; @Id public String getRollno() { return rollNo; } @Id public String getSem() { return sem; } ...... }

ProDev IT Solutions, Indore. | Amit Bhandari -9425431512

12

You might also like