Set

This topic applies to Java version only 

Replicating a Set of objects is simple and is similar to the List example.

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

Use the set tag.

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.set.Pilot"> 09 <id column="typed_id" type="long"> 10 <generator class="native"/> 11 </id> 12 13 <property name="name"/> 14 15 <set name="cars" table="cars"> 16 <key column="pilotId"/> 17 <one-to-many class="f1.collection.Car"/> 18 </set> 19 </class> 20</hibernate-mapping>

Do the replication.

SetExample.java: main
01public class SetExample { 02 public static void main(String[] args) { 03 new File("SetExample.yap").delete(); 04 05 System.out.println("Running Set example."); 06 07 ExtDb4o.configure().generateUUIDs(Integer.MAX_VALUE); 08 ExtDb4o.configure().generateVersionNumbers(Integer.MAX_VALUE); 09 10 ObjectContainer objectContainer = Db4o.openFile("SetExample.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 HashSet(); 24 pilot.cars.add(car1); 25 pilot.cars.add(car2); 26 27 objectContainer.set(pilot); 28 objectContainer.commit(); 29 30 Configuration config = new Configuration().configure("f1/collection/set/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("SetExample.yap").delete(); 43 }