![]() | ![]() |
![]() |
| Project | Ver 1.1 | Ver 1.2 | JDO | JPA | Guides | Tools |
| 1.2 | Persistence | JDO ORM | JPA ORM | Runtime | JDO Runtime | JPA Runtime | Extensions | Developer |
![]() When persisting a class you need to decide how it is to be mapped to the datastore. By this we mean which fields of the class are persisted. JPOX knows how to persist certain Java types and so you bear this list in mind when deciding which fields to persist. Also please note that JPA cannot persist static or final fields. Let's take a sample class as an example
public class Hotel
{
private long id; // identity
private String name;
private String address;
private String telephoneNumber;
private int numberOfRooms;
private String hotelNickname;
private Set rooms = new HashSet();
private Manager manager;
...
}We have a series of fields and we want to persist all fields apart from hotelNickname which is of no real use in our system. In addition, we want our Hotel class to be detachable, meaning that we can detach objects of that type update them in a different part of our system, and the attach them again. We can define this basic persistence information in 3 ways - with XML MetaData, with JDK1.5 Annotations or with a mix of MetaData and Annotations. We show all ways here.
To achieve the above aim we define our Meta-Data like this
<entity class="org.jpox.test.Hotel">
<attributes>
<id name="id"/>
<basic name="name"/>
<basic name="address"/>
<basic name="telephoneNumber"/>
<basic name="numberOfRooms"/>
<transient name="hotelNickname"/>
<one-to-many name="rooms" target-entity="org.jpox.test.Room"/>
<one-to-one name="manager" target-entity="org.jpox.test.Manager"/>
</attributes>
</entity>Note the following
So it is really very simple. This first step is to define the basic persistence of a class. If you are using a datastore (such as RDBMS) that requires detailed mapping information then you now need to proceed to the JPA Schema Mapping Guide. If however you are using a datastore that doesn't need such information (such as DB4O) then you have defined the persistence of your class. See also :-
Here we are using JDK1.5 or higher and we annotate the class directly using JPA Annotations We annotate the class like this
@Entity
public class Hotel
{
@Id
private long id;
@Basic
private String name;
@Basic
private String address;
@Basic
private String telephoneNumber;
@Basic
private int numberOfRooms;
@Transient
private String hotelNickname;
@OneToMany(target="Room")
private Set rooms = new HashSet();
@OneToOne(target="Manager)
private Manager manager;
...
}
See also :-
If we are using JDK 1.5+ we can take advantage of Annotations, but we want to take into account the disadvantage of Annotations, namely that we may want to deploy our application to multiple datastores. This means that we would be extremely unwise to specify ORM information in Annotations. With this in mind we decide that we will specify just the basic persistence information (which classes/fields are persisted etc) using Annotations, and the remainder will go in MetaData. The order of precedence for persistence information is
So anything specified in MetaData will override all Annotations. |