JPA Annotations - Contents:
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
| @Entity public 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
|
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; ... } |
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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| @Entity @Table (name = "contact" ) public class Contact implements Serializable { @ManyToOne @JoinColumn (name = "companyId" ) private Company company; ... } @Entity @Table (name = "company" ) public class Company implements Serializable { @ManyToOne @JoinColumn (name = "statusId" ) private CompanyStatus status; ... } |
@OneToMany
|
1
2
3
4
5
6
7
8
9
10
11
| @Entity @Table (name = "company" ) public class Company implements Serializable { @OneToMany (mappedBy = "company" , fetch = FetchType.EAGER) @OrderBy ( "firstName asc" ) private Set contacts; ... } |
@ManyToMany
|
@PrimaryKeyJoinColumn
@PrimaryKeyJoinColumn annotation is used for associated entities sharing the same primary key. See OneToOne section for details.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| @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; ... } |
@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.
1
2
3
| @ManyToOne @JoinColumn (name = "statusId" ) private CompanyStatus 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.
1
2
3
4
| @OneToOne @MapsId @JoinColumn (name = "contactId" ) private Contact 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
- 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
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 { ... } |
1
2
3
4
5
6
7
| @Entity @Inheritance (strategy=InheritanceType.JOINED) public class Boat implements Serializable { ... } @Entity @PrimaryKeyJoinColumn public 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.
1
2
| @Entity @Inheritance (strategy=InheritanceType.SINGLE_TABLE) |
@DiscriminatorColumn
See Hibernate Inheritance Mapping Annotations section for details.
1
2
3
| @Entity @Inheritance (strategy=InheritanceType.SINGLE_TABLE) @DiscriminatorColumn (name= "planetype" , discriminatorType=DiscriminatorType.STRING ) |
@DiscriminatorValue
See Hibernate Inheritance Mapping Annotations section for details.
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 { ... } |
No comments :
Post a Comment