The Top 5 New Annotations in JPA 2
Enhancements to Existing JPA 1.x Annotations
In this section, we will describe some changes made to the existing JPA 1.x annotations.
Additional Support for Embeddable Classes
Embeddable classes in JPA 2 can contain other embeddable classes and can also contain relationships to other entities, which was not possible in JPA 1.x. The Employee
entity has an embedded PersonalInfo
type which has a nested embeddable object Address
. The PersonalInfo
entity also has a bidirectional ManyToOne
relationship with the Project
entity. The mappedBy
attribute of OneToMany
determines the field that owns the relationship. In the following example it is determined by the attribute of the Employee
entity followed by the dot (.
) operator and the attribute of the PersonalInfo
entity.
@EntityEmployee{@Embeddedprivate PersonalInfo personalInfo;// Other properties and methods}@EmbeddablePersonalInfo{@JoinColumn(name = "projectId", referencedColumnName = "id")@ManyToOne(optional = false, cascade=CascadeType.ALL)private Project project; @Embeddableprivate Address address;// Other Properties and methods} @EntityAddress{ // All the Properties and methods} @EntityProject {@OneToMany(cascade = CascadeType.ALL, mappedBy = "personalInfo.project") private List<Employee> employeeCollection; // Other code}
@JoinTable for OneToOne Relationships
Suppose the Employee
entity has a OneToOne
relationship with the EmployeeRecord
entity. In the EMPLOYEERECORD
table a foreign key is maintained which corresponds to the EMPLOYEE
table. But when the foreign keys are maintained in a separate table, you can decorate the EmployeeRecord
instance with @JoinTable
, which specifies the name of the join table. Here's an example:
@Entity@Tablepublic class Employee {// Other Code @OneToOne(cascade = CascadeType.ALL) @JoinTable(name = "employee_record", joinColumns = {@JoinColumn(name="employeeId", referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "recordId", referencedColumnName = "id")}) private EmployeeRecord employeeRecord;}
@JoinColumn for Unidirectional OneToMany Relationships
JPA 1.x supports unidirectional OneToMany
mapping by using a join table that stores the two foreign keys of the participating entities. JPA 2 avoids the usage of this join table by storing the foreign key in the many-to-one side.
Consider the Employee
entity, which has a OneToMany
unidirectional relationship with the File
entity by having a List
object. Theoretically the File
entity should not have any relationship to the Employee
entity, but it is not possible to insert a File
object through the Employee
entity without having the relationship. Hence, the File
entity will have an Employee
type that will not be part of the relationship. The @JoinColumn
annotation specifies the foreign key in the File
table, and this snippet shows very clearly that @JoinTable
is not required to achieve the same result:
@Entity@Tablepublic class Employee {// Other Code@OneToMany(cascade=CascadeType.ALL) @JoinColumn(name="empId") private List<File> fileList;}
Conclusion
In this article, we have discussed the new annotations introduced in JPA 2 with code examples. All these features have been implemented in EclipseLink. We also covered some additional capabilities provided by the existing annotations to make JPA perform a better and more convenient ORM.
Acknowledgements
The authors would like to sincerely thank Mr. Subrahmanya SV (VP, ECOM Research Group, E&R) for his constant encouragement and Ms. Sangeetha S. for providing the idea for this article, as well as guidance, valuable comments and reviews.
About the Authors
Mahalakshmi K. works at the E-Commerce Research Labs at Infosys Technologies Ltd. on Java EE technologies. Her core interest is in researching image processing, and she is also involved in the design and development of Java EE applications using Hibernate and JPA.
Nitin KL works at the E-Commerce Research Labs at Infosys Technologies. He is involved in design and development of Java EE applications using Hibernate, iBATIS, and JPA.
Page 2 of 2
This article was originally published on August 13, 2010