| Annotation | Package Detail/Import statement |
|---|---|
| @Entity | import javax.persistence.Entity; |
| @Table | import javax.persistence.Table; |
| @Column | import javax.persistence.Column; |
| @Id | import javax.persistence.Id; |
| @GeneratedValue | import javax.persistence.GeneratedValue; |
| @Version | import javax.persistence.Version; |
| @OrderBy | import javax.persistence.OrderBy; |
| @Transient | import javax.persistence.Transient; |
| @Lob | import javax.persistence.Lob; |
| Hibernate Association Mapping Annotations | |
| @OneToOne | import javax.persistence.OneToOne; |
| @ManyToOne | import javax.persistence.ManyToOne; |
| @OneToMany | import javax.persistence.OneToMany; |
| @ManyToMany | import javax.persistence.ManyToMany; |
| @PrimaryKeyJoinColumn | import javax.persistence.PrimaryKeyJoinColumn; |
| @JoinColumn | import javax.persistence.JoinColumn; |
| @JoinTable | import javax.persistence.JoinTable; |
| @MapsId | import javax.persistence.MapsId; |
| Hibernate Inheritance Mapping Annotations | |
| @Inheritance | import javax.persistence.Inheritance; |
| @DiscriminatorColumn | import javax.persistence.DiscriminatorColumn; |
| @DiscriminatorValue | import javax.persistence.DiscriminatorValue; |
@Entity
Annotate all your entity beans with @Entity.
1
2
3
4
| @Entitypublic class Company implements Serializable {...} |
@Table
Specify the database table this Entity maps to using the name attribute of @Table annotation. In the example below, the data will be stored in 'company' table in the database.
1
2
3
4
5
| @Entity@Table(name = "company")public class Company implements Serializable {...} |
@Column
Specify the column mapping using @Column annotation.
1
2
3
4
5
6
7
8
9
| @Entity@Table(name = "company")public class Company implements Serializable { @Column(name = "name") private String name; ...} |
@Id
Annotate the id column using @Id.
1
2
3
4
5
6
7
8
9
10
| @Entity@Table(name = "company")public class Company implements Serializable { @Id @Column(name = "id") private int id; ...} |
@GeneratedValue
Let database generate (auto-increment) the id column.
1
2
3
4
5
6
7
8
9
10
11
| @Entity@Table(name = "company")public class Company implements Serializable { @Id @Column(name = "id") @GeneratedValue private int id; ...} |
@Version
Control versioning or concurrency using @Version annotation.
1
2
3
4
5
6
7
8
9
10
| @Entity@Table(name = "company")public class Company implements Serializable { @Version @Column(name = "version") private Date version; ...} |
@OrderBy
Sort your data using @OrderBy annotation. In example below, it will sort all contacts in a company by their firstname in ascending order.
1
2
| @OrderBy("firstName asc")private Set contacts; |
@Transient
Annotate your transient properties with @Transient.@Lob
Annotate large objects with @Lob.Hibernate Association Mapping Annotations
Example App DB Schema
The database for this tutorial is designed to illustrate various association mapping concepts.
In RDBMS implementations, entities are joined using the following ways:
- Shared Primary Key
- Foreign Key
- Association Table
- Tables company and companyDetail have shared values for primary key. It is a one-to-one assoication.
- Tables contact and contactDetail are linked through a foreign key. It is also a one to one association.
- Tables contact and company are linked through a foriegn key in many-to-one association with contact being the owner.
- Tables company and companyStatus are linked through a foreign key in many-to-one association with company being the owner.
@OneToOne
- Use @PrimaryKeyJoinColumn for associated entities sharing the same primary key.
- Use @JoinColumn & @OneToOne mappedBy attribute when foreign key is held by one of the entities.
- Use @JoinTable and mappedBy entities linked through an association table.
- Persist two entities with shared key using @MapsId
Notice that the id property of CompanyDetail is NOT annotated with @GeneratedValue. It will be populated by id value of Company.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| @Entity@Table(name = "company")public class Company implements Serializable { @Id @Column(name = "id") @GeneratedValue private int id; @OneToOne(cascade = CascadeType.MERGE) @PrimaryKeyJoinColumn private CompanyDetail companyDetail; ...}@Entity@Table(name = "companyDetail")public class CompanyDetail implements Serializable { @Id @Column(name = "id") private int id; ...} |
For entities Contact and ContactDetail linked through a foriegn key, we can use @OneToOne and @JoinColumn annotations. In example below, the id genereated for Contact will be mapped to 'contact_id' column of ContactDetail table. Please note the usage of @MapsId for the same.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| @Entity@Table(name = "contactDetail")public class ContactDetail implements Serializable { @Id @Column(name = "id") @GeneratedValue private int id; @OneToOne @MapsId @JoinColumn(name = "contactId") private Contact contact; ...}@Entity@Table(name = "contact")public class Contact implements Serializable { @Id @Column(name = "ID") @GeneratedValue private Integer id; @OneToOne(mappedBy = "contact", cascade = CascadeType.ALL) private ContactDetail contactDetail; ....} |
The rationale to have one relationship as uni-directional and other as bi-directional in this tutorial is to illustrate both concepts and their usage. You can opt for uni-directional or bi-directional relationships to suit your needs.
@ManyToOne
- Use @JoinColumn when foreign key is held by one of the entities.
- Use @JoinTable for entities linked through an association table.
- The two examples below illustrate many-to-one relationships. Contact to Company and Company to CompanyStatus.
Many contacts can belong to a company. Similary many companies can share the same status (Lead, Prospect, Customer)
- there will be many companies that are currently leads.
1234567891011121314151617181920212223
@Entity@Table(name ="contact")publicclassContactimplementsSerializable {@ManyToOne@JoinColumn(name ="companyId")privateCompany company;...}@Entity@Table(name ="company")publicclassCompanyimplementsSerializable {@ManyToOne@JoinColumn(name ="statusId")privateCompanyStatus status;...}@OneToMany
- Use mappedBy attribute for bi-directional associations with ManyToOne being the owner.
- OneToMany being the owner or unidirectional with foreign key - try to avoid such associations but can be achieved with @JoinColumn
- @JoinTable for Unidirectional with association table
Again, for this tutorial, we have kept Company to CompanyStatus relationship as uni-directional.1234567891011@Entity@Table(name ="company")publicclassCompanyimplementsSerializable {@OneToMany(mappedBy ="company", fetch = FetchType.EAGER)@OrderBy("firstName asc")privateSet contacts;...}@ManyToMany
- Use @JoinTable for entities linked through an association table.
- Use mappedBy attribute for bi-directional association.
@PrimaryKeyJoinColumn
@PrimaryKeyJoinColumn annotation is used for associated entities sharing the same primary key. See OneToOne section for details.123456789101112131415@Entity@Table(name ="company")publicclassCompanyimplementsSerializable {@Id@Column(name ="id")@GeneratedValueprivateintid;@OneToOne(cascade = CascadeType.MERGE)@PrimaryKeyJoinColumnprivateCompanyDetail companyDetail;...}@JoinColumn
Use @JoinColumn annotation for one-to-one or many-to-one associations when foreign key is held by one of the entities. We can use @OneToOne or @ManyToOne mappedBy attribute for bi-directional relations. Also see OneToOne and ManyToOne sections for more details.123@ManyToOne@JoinColumn(name ="statusId")privateCompanyStatus status;@JoinTable
Use @JoinTable and mappedBy for entities linked through an association table.@MapsId
Persist two entities with shared key (when one entity holds a foreign key to the other) using @MapsId annotation. See OneToOne section for details.1234@OneToOne@MapsId@JoinColumn(name ="contactId")privateContact contact;Hibernate Inheritance Mapping Annotations
To understand Inheritance Mapping annotations, you must first understand Inheritance Mapping in Hiberate in detail. Once you understand Inheritance mapping concepts, please review below for annotations to be used. - table per class hierarchy - single table per Class Hierarchy Strategy: the <subclass> element in Hibernate
1
2
3
4
5
6
7
8
9
10
| @Entity@Inheritance(strategy=InheritanceType.SINGLE_TABLE)@DiscriminatorColumn(name="planetype", discriminatorType=DiscriminatorType.STRING )@DiscriminatorValue("Plane")public class Plane { ... }@Entity@DiscriminatorValue("A320")public class A320 extends Plane { ... } |
- table per class/subclass - joined subclass Strategy: the <joined-subclass> element in Hibernate
- table per concrete class - table per Class Strategy: the <union-class> element in Hibernate
1
2
3
4
5
6
7
| @Entity@Inheritance(strategy=InheritanceType.JOINED)public class Boat implements Serializable { ... }@Entity@PrimaryKeyJoinColumnpublic class Ferry extends Boat { ... } |
1
2
3
| @Entity@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)public class Flight implements Serializable { ... } |
Note: This strategy does not support the IDENTITY generator strategy: the id has to be shared across several tables. Consequently, when using this strategy, you should not use AUTO nor IDENTITY.
@Inheritance
See Hibernate Inheritance Mapping Annotations section for details.12@Entity@Inheritance(strategy=InheritanceType.SINGLE_TABLE)@DiscriminatorColumn
See Hibernate Inheritance Mapping Annotations section for details.123@Entity@Inheritance(strategy=InheritanceType.SINGLE_TABLE)@DiscriminatorColumn(name="planetype", discriminatorType=DiscriminatorType.STRING )@DiscriminatorValue
See Hibernate Inheritance Mapping Annotations section for details.12345678910@Entity@Inheritance(strategy=InheritanceType.SINGLE_TABLE)@DiscriminatorColumn(name="planetype", discriminatorType=DiscriminatorType.STRING )@DiscriminatorValue("Plane")publicclassPlane { ... }@Entity@DiscriminatorValue("A320")publicclassA320extendsPlane { ... }References:
- Hibernate Annotations: http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/
- Inheritance Mapping Reference: http://docs.jboss.org/hibernate/core/3.5/reference/en/html/inheritance.html
No comments:
Post a Comment