![]() | ![]() |
![]() |
| Project | Ver 1.1 | Ver 1.2 | JDO | JPA | Guides | Tools |
| 1.1 | Preparation | O/R Mapping | 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. 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 2 ways - with XML MetaData or with JDK1.5 Annotations. We show both ways here.
To achieve the above aim we define our Meta-Data like this
<class name="Hotel" detachable="true">
<field name="id" primary-key="true"/>
<field name="name"/>
<field name="address"/>
<field name="telephoneNumber"/>
<field name="numberOfRooms"/>
<field name="hotelNickname" persistence-modifier="none"/>
<field name="rooms">
<collection element-type="Room"/>
</field>
<field name="manager"/>
</class>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 Schema Mapping Guide. If however you are using a datastore that doesnt 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 JDO Annotations We annotate the class like this
@PersistenceCapable
public class Hotel
{
@Field(primary-key="true")
private long id;
@Field
private String name;
@Field
private String address;
@Field
private String telephoneNumber;
@Field
private int numberOfRooms;
@Field(persistence-modifier=FieldPersistenceModifier.NONE)
private String hotelNickname;
@Field
@Collection(element-type="Room")
private Set rooms = new HashSet();
@Field
private Manager manager;
...
}
See also :-
With JDO persistence all classes that are persisted have to be identified in MetaData or annotations as shown above. In addition, if any of your other classes access the fields of these persistable classes directly then these other classes should be defined as PersistenceAware. You do this as follows <class name="MyClass" persistence-modifier="persistence-aware"/> or with annotations
@PersistenceAware
public class MyClass
{
...
} |