Well, the API javadoc for Hibernate says "Persist all reachable transient objects, reusing the current identifier values." For exactly what it does, see the source. :) (net.sf.hibernate.impl.SessionImpl)
I haven't seen any code examples, but I can show you how I'm using it. Here's an example of a stateless session EJB call that replicates objects from one server to another.
Server code:
Code:
public List synchSiteDataObjects(List replicants)
throws RemoteException {
List modifiedSDOs = null;
Session session = getSession();
try {
SynchManager synchManager = SynchManager.getInstance();
Synch lastSynch = synchManager.getLast(session);
try {
Transaction trans = session.beginTransaction();
for(Iterator i = replicants.iterator(); i.hasNext();) {
session.replicate(i.next(), ReplicationMode.LATEST_VERSION);
}
trans.commit();
} catch(HibernateException he) {
trans.rollback();
LOG.error("replicate failed!", he);
throw he;
}
//One of the objects replicated should have been a new Synch object.
Synch newSynch = synchManager.getLast(session);
modifiedSDOs = synchManager.getModified(
lastSynch,
newSynch,
session
);
} catch(HibernateException he) {
LOG.fatal("synchSiteDataObjects failed!", he);
throw new RemoteException("synchSiteDataObjects failed!", he);
}}
return modifiedSDOs;
}
Client code:
Code:
public static final void synchSiteDataObjects(
Site site,
Session session
)
throws RemoteException,
NamingException,
CreateException,
UnknownHostException,
HibernateException {
//Get remote interface first. If this fails we want to
//throw exceptions quickly.
SynchRemote bean = getSynchRemote(site);
SynchManager synchManager = SynchManager.getInstance();
Synch lastSynch = synchManager.getLast(
site,
session
);
//Include a new Synch object to restrict objects replicated
//and also be replicated to server. (Saves new Synch object to session.)
Synch newSynch = SynchFactory.getInstance()
.create(
site,
session
);
List modifiedSDOs = synchManager.getModified(
lastSynch,
newSynch,
session
);
List replicants = bean.synchSiteDataObjects(modifiedSDOs);
try {
Transaction trans = session.beginTransaction();
for(Iterator i = replicants.iterator(); i.hasNext();) {
session.replicate(i.next(), ReplicationMode.LATEST_VERSION);
}
trans.commit();
} catch(HibernateException he) {
trans.rollback();
LOG.error("replicate failed!", he);
throw he;
}
}
In this example a Site object describes the location (ip address) of the server I want to replicate to. I use a Synch object who'se only attribute is a timestamp typed version field to limit the objects replicated. The SynchFactory creates a new Synch object. The SynchManager provides convenience methods for getting the latest Synch object (the one with the most recent timestamp) and gathering all objects which have been modified between the timestamps of two Synch objects. (All of my persistent objects have a timestamp version.) One of the objects in the replicants list is a Synch object generated by the client.
How well does this work? Don't know yet. All of my persistent objects are mapped using <joined-subclass> and there seems to be a problem with replicate() and joined-subclass.
http://forum.hibernate.org/viewtopic.php?t=926835