In this article, we will introduce the storage and operation of structured objects in db4o, and first introduce the diversity relationship. In the diversity relationship, the object contains fields in the form of object sets. Db4o can easily process diversity. We will be further familiar with db4o's deep processing of cascade updates and activation.
Process diverse relationships
A comfortable family life can lead to one or more "small people" coming to this family. However, before adding a child to the family, I want to ensure that my Person has a place to live. I want to give them a work place or a good summer vacation home. An Address type should be able to solve all three problems.
Listing 1. Add an Address type to the Person class
Package com. tedneward. model;
Public class Address
{
Public Address ()
{}
Public Address (String street, String city, String state, String zip)
{
This. street = street; this. city = city;
This. state = state; this.zip = zip;
}
Public String toString ()
{
Return "[Address:" + "street =" + street + "" + "city =" + city + "" +
"State =" + state + "" + "zip =" + zip + "]";
}
Public int hashCode ()
{
Return street. hashCode () & city. hashCode ()&
State. hashCode () & zip. hashCode ();
}
Public boolean equals (Object obj)
{
If (obj = this)
Return this;
If (obj instanceof Address)
{
Address rhs = (Address) obj;
Return (this. street. equals (rhs. street )&&
This. city. equals (rhs. city )&&
This. state. equals (rhs. state )&&
This.zip.equals(rhs.zip ));
}
Else
Return false;
}
Public String getStreet () {return this. street ;}
Public void setStreet (String value) {this. street = value ;}
Public String getCity () {return this. city ;}
Public void setCity (String value) {this. city = value ;}
Public String getState () {return this. state ;}
Public void setState (String value) {this. state = value ;}
Public String getZip () {return this.zip ;}
Public void setZip (String value) {this.zip = value ;}
Private String street;
Private String city;
Private String state;
Private String zip;
}
We can see that Address is just a simple data object. Adding it to the Person class means that Person will have an Address array named addresses as the field. The first address is the home address, the second is the work address, and the third (if not null) is the holiday home address. Of course, these are all set to protected for future encapsulation through methods.
After completing these settings, you can now enhance the Person class to support children, so I will define a new field for Person: A Person ArrayList, which also has some related methods, for proper encapsulation.
Next, because most children have their parents, I will add two fields to indicate the mother and father, and add the appropriate accessor/mutator method. I will add a new method for the Person class so that a new Person can be created. This method has an appropriate name, namely, haveBaby. In addition, some business rules are added to support the biological needs of children, and the new small Person is added to the children ArrayList created for the mother and father fields. After that, return the baby to the caller.
Listing 2 shows that the newly defined Person class can handle this diversity relationship.
List 2. Family life defined as a diversity relationship
Package com. tedneward. model;
Import java. util. List;
Import java. util. ArrayList;
Import java. util. Iterator;
Public class Person
{
Public Person ()
{}
Public Person (String firstName, String lastName, Gender gender, int age, Mood mood)
{
This. firstName = firstName;
This. lastName = lastName;
This. gender = gender;
This. age = age;
This. mood = mood;
}
Public String getFirstName () {return firstName ;}
Public void setFirstName (String value) {firstName = value ;}
Public String getLastName () {return lastName ;}
Public void setLastName (String value) {lastName = value ;}
Public Gender getGender () {return gender ;}
Public int getAge () {return age ;}
Public void setAge (int value) {age = value ;}
Public Mood getMood () {return mood ;}
Public void setMood (Mood value) {mood = value ;}
Public Person getSpouse () {return spouse ;}
Public void setSpouse (Person value ){
// A few business rules
If (spouse! = Null)
Throw new IllegalArgumentException ("Already married! ");
If (value. getSpouse ()! = Null & value. getSpouse ()! = This)
Throw new IllegalArgumentException ("Already married! ");
Spouse = value;
// Highly sexist business rule
If (gender = Gender. FEMALE)
This. setLastName (value. getLastName ());
// Make marriage reflexive, if its not already set that way
If (value. getSpouse ()! = This)
Value. setSpouse (this );