This topic applies to Java version only
This is a one-to-one association example.
The following persistent classes are used:
1package f1.one_to_one; 2
3
public class Helmet { 4
String model; 5
}
1package f1.one_to_one; 2
3
public class Pilot { 4
String name; 5
Helmet helmet; 6
}
A one-to-one association to another persistent class is declared using a one-to-one element:
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" 08
default-cascade="save-update"> 09
<class name="f1.one_to_one.Pilot"> 10
<id column="typed_id" type="long"> 11
<generator class="foreign"> 12
<param name="property">helmet</param> 13
</generator> 14
</id> 15
<property name="name"/> 16
<one-to-one name="helmet"/> 17
</class> 18
</hibernate-mapping>
Remember to add mappings in hibernate.cfg.xml:
<mapping resource="f1/one_to_one/Pilot.hbm.xml"/>
<mapping resource="f1/one_to_one/Helmet.hbm.xml"/>
The code to run the replication is provided below:
01public class OneToOneExample { 02
public static void main(String[] args) { 03
new File("OneToOneExample.db4o").delete(); 04
05
System.out.println("Running OneToOneExample example."); 06
07
ExtDb4o.configure().generateUUIDs(Integer.MAX_VALUE); 08
ExtDb4o.configure().generateVersionNumbers(Integer.MAX_VALUE); 09
10
ObjectContainer objectContainer = Db4o.openFile("OneToOneExample.db4o"); 11
12
//create and save the pilot. Helmet is saved automatically. 13
Helmet helmet = new Helmet(); 14
helmet.model = "Robuster"; 15
16
Pilot pilot = new Pilot(); 17
pilot.name = "John"; 18
pilot.helmet = helmet; 19
20
objectContainer.set(pilot); 21
objectContainer.commit(); 22
23
// Perform the replication 24
Configuration config = new Configuration().configure("f1/one_to_one/hibernate.cfg.xml"); 25
26
ReplicationSession replication = HibernateReplication.begin(objectContainer, config); 27
ObjectSet changed = replication.providerA().objectsChangedSinceLastReplication(); 28
29
// Here helmet is cascaded from pilot and is replicated automatically. 30
while (changed.hasNext()) 31
replication.replicate(changed.next()); 32
33
replication.commit(); 34
replication.close(); 35
objectContainer.close(); 36
37
new File("OneToOneExample.db4o").delete(); 38
}