ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Agile Object to Relational Database Replication with db4o
Pages: 1, 2, 3, 4, 5, 6, 7, 8

Now we need to create some instances, and store these in a db4o database. Actually, we'll store them in a number of separate databases, as you would with db4o databases on separate disconnected mobile devices. These separate databases can then be merged into the MySQL database. Two databases will be enough to demonstrate the point.



In db4o, you can create a database very easily within your application code. Adding the single db4o.jar file as a library to your Java project gives you access to the API classes. The database itself is contained in a single file, usually given the extension .yap. The following line of code opens an existing database file, or creates a new one if it doesn't exist:

ObjectContainer objectContainer = Db4o.openFile("c:/mobileone.yap");

ObjectContainer is a db4o API class that provides a handle to allow you to access the database. Calls to store, update, or retrieve objects from the database are made through the ObjectContainer reference.

The following code creates two Customer objects and two Address objects. Each Customer is associated with one of the Address objects. The Customer objects are stored simply by calling the method ObjectContainer#set. The database doesn't have to be told that it is going to store this type of object--it can store any kind of object with no need to define the schema in advance. Objects are stored as Java objects.

What about storing the Address objects? Well, because each one is a field of a Customer, storing the Customer also stores the Address, so we don't need to store them explicitly.

ExtDb4o.configure().generateVersionNumbers(Integer.MAX_VALUE);
ExtDb4o.configure().generateUUIDs(Integer.MAX_VALUE);
ObjectContainer objectContainer = Db4o.openFile("c:/mobileone.yap");

Address a1 = new Address();
a1.setStreet1("1 First Street");
a1.setStreet2("Downtown");
a1.setCity("Pittsburgh");
a1.setZip("PA 15121");
a1.setCountry("USA");

Address a2 = new Address();
a2.setStreet1("2 Second Street");
a2.setStreet2("Uptown");
a2.setCity("Seattle");
a2.setZip("WA 98200");
a2.setCountry("USA");

Customer c1 = new Customer();
c1.setTitle("Mr");
c1.setFirstName("Ben");
c1.setLastName("Roethlisberger");
c1.setEmail("ben@steelers.com");
c1.setPhoneNumber("1-412-123-4567");
c1.setFaxNumber("1-412-987-6543");
c1.setCreditLine(25000);
c1.setContactCode("Joe");

Customer c2 = new Customer();c2.setTitle("Mr");
c2.setFirstName("Matt");
c2.setLastName("Hasselbeck");
c2.setEmail("matt@seahawks.com");
c2.setPhoneNumber("1-206-123-4567");
c2.setFaxNumber("1-206-987-6543");
c2.setCreditLine(16000);
c2.setContactCode("Joe");

c1.setAddress(a1);
c2.setAddress(a2);

objectContainer.set(c1);
objectContainer.set(c2);
objectContainer.commit();
objectContainer.close();

There are a couple of db4o configuration calls made before the database file is opened.

  • The first call, generateVersionNumbers, specifies that the database should store version numbers with objects, which is clearly important for synchronization.
  • The second call, generateUUIDs, specifies how the database will assign IDs to its objects. An object database stores a unique ID with each object, which is not related to the value of any field of the object. It does not rely on primary keys to uniquely identify entities. If the objects are to be transferred between databases, then the IDs need to be Unique Universal IDs (UUIDs) that are guaranteed to be globally unique. If not, then shorter IDs that are only unique within a single database can be used. Note also that the id field in each object is left as null; this field is there for the benefit of MySQL, and is not needed by the db4o database.

Pages: 1, 2, 3, 4, 5, 6, 7, 8

Next Pagearrow