Map

This topic applies to Java version only 

Replication supports replicating a Map of objects.

Pilot.java
1package f1.collection.map; 2 3import java.util.Map; 4 5public class Pilot { 6 String name; 7 Map cars; 8}

Use the map element to define the Map in the hbm file:

Pilot.hbm.xml
01<?xml version="1.0"?> 02 03<!DOCTYPE hibernate-mapping PUBLIC 04 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 05 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 06 07<hibernate-mapping default-access="field" default-lazy="false" default-cascade="save-update"> 08 <class name="f1.collection.map.Pilot"> 09 <id column="typed_id" type="long"> 10 <generator class="native"/> 11 </id> 12 13 <property name="name"/> 14 15 <map name="cars" table="cars"> 16 <key column="pilotId"/> 17 <map-key type="string"/> 18 <one-to-many class="f1.collection.Car"/> 19 </map> 20 </class> 21</hibernate-mapping>

Do the replication:

MapExample.java: main
01public class MapExample { 02 public static void main(String[] args) { 03 new File("MapExample.yap").delete(); 04 05 System.out.println("Running Map example."); 06 07 ExtDb4o.configure().generateUUIDs(Integer.MAX_VALUE); 08 ExtDb4o.configure().generateVersionNumbers(Integer.MAX_VALUE); 09 10 ObjectContainer objectContainer = Db4o.openFile("MapExample.yap"); 11 12 Pilot pilot = new Pilot(); 13 pilot.name = "John"; 14 15 Car car1 = new Car(); 16 car1.brand = "BMW"; 17 car1.model = "M3"; 18 19 Car car2 = new Car(); 20 car2.brand = "Mercedes Benz"; 21 car2.model = "S600SL"; 22 23 pilot.cars = new HashMap(); 24 pilot.cars.put("car1", car1); 25 pilot.cars.put("car2", car2); 26 27 objectContainer.set(pilot); 28 objectContainer.commit(); 29 30 Configuration config = new Configuration().configure("f1/collection/map/hibernate.cfg.xml"); 31 32 ReplicationSession replication = HibernateReplication.begin(objectContainer, config); 33 ObjectSet changed = replication.providerA().objectsChangedSinceLastReplication(); 34 35 while (changed.hasNext()) 36 replication.replicate(changed.next()); 37 38 replication.commit(); 39 replication.close(); 40 objectContainer.close(); 41 42 new File("MapExample.yap").delete(); 43 }