![]() | ![]() |
![]() |
| Project | Ver 1.1 | Ver 1.2 | JDO | JPA | Guides | Tools |
| 1.1 | Preparation | O/R Mapping | Runtime | Extensions | Developer |
![]() Whilst JPOX was always intended as a JDO implementation, it is intended also to implement the Java Persistence API (JPA) specification when time permits. This specification is a subset of the JDO2 specification but does provide for a few JDK 1.5 features that can be useful in some situations. One of these is the use of JDK 1.5 annotations. JPOX provides a level of support for JPA annotations from JPOX version 1.1.2 onwards and this support is currently considered experimental work-in-progress so please report any issues found. Things to bear in mind when considering using this facility
The principal idea behind annotations in JPOX is that you can store the basic persistence information that you embody in a JDO MetaData file instead in the Java file alongside the fields of the class. We do not recommend storing the information storable in an ORM MetaData file as annotations since this is the type of information that you would typically change at deployment time to match the RDBMS in use etc. Some general rules for annotations are
Let's start with some sample classes, and add some annotations.
public class Person
{
String firstName;
String lastName;
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
}
public class Manager extends Person
{
HashSet employees;
Department department;
public Department getDepartment()
{
return department;
}
}So we firstly want to mark Person and Manager as persistent. We do this using the @Entity JPA annotation.
@Entity
public class Person
{
...
}
@Entity
public class Manager extends Person
{
...
}Now that we know our classes are going to be persisted ("PersistenceCapable") we need to configure how the fields will be persisted. By default our simple fields like "firstName", "lastName" will be persisted, but we can reenforce this using the @Basic annotation
@Entity
public class Person
{
@Basic
String firstName;
@Basic
String lastName;
...
}We should point out that we could have added the annotations to the "getter" method instead of the field itself. Both places serve the same purpose. Next we want to configure that our Person and Manager classes each have their own table. We use the @Inheritance annotation
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Person
{
...
}
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Manager extends Person
{
...
}It is worth mentioning that Eclipse users can benefit from the "Dali" plugin that allows generation of JPA annotations. Please refer to the JPOX Eclipse-Dali tutorial.
Enhancing the annotated classes operates in a very similar way as enhancing a normal class with MetaData except that you supply the class files and not MetaData files to JPOX Enhancer.
Generating the schema for annotated classes operates in a very similar way as generating the schema for a normal class with MetaData except that you supply the class files and not MetaData files to JPOX SchemaTool.
This section lists the JPA annotations supported by JPOX, as well as the custom annotations added by JPOX to fill holes that JPA doesnt provide for (where JDO is a more complete specification than JPA).
|