JDO支持一种持久化模型,这这种模型中持久性自动传播到引用的对象。这种机制经常称为“延伸持久(persistence by reachability)”或者“传递持久(transitive persistence)”。这意味着一旦一个已经持久化的对象引用了一个临时对象,这个临时对象自动变成持久化的。对于JDBC编程者,这个模型可能很希奇,但是他们会发现这是大多数情况下编程者希望从持久化框架中得到的支持。
例子: pmf = (PersistenceManagerFactory) (Class.forName("com.libelis.lido.PersistenceManagerFactory").newInstance()); pmf.setConnectionDriverName("versant"); pmf.setConnectionURL(dbName); pm = pmf.getPersistenceManager(); tx = pm.currentTransaction(); tx.begin(); Provider aProvider = new Provider("LIBeLIS"); pm.makerPersistent(aProvider); // aProvider now persists Address anAddress = new Address("25 rue Paul Barruel", "France", "Paris"); aProvider.address = anAddress ; // anAddress now persists tx.commit(); pm.close();
例子 pmf = (PersistenceManagerFactory) (Class.forName("com.libelis.lido.PersistenceManagerFactory").newInstance()); pmf.setConnectionDriverName("versant"); pmf.setConnectionURL(dbName); pm = pmf.getPersistenceManager(); tx = pm.currentTransaction(); tx.begin(); Provider aProvider = new Provider("LIBeLIS"); pm.makerPersistent(aProvider); Address anAddress = new Address("25 rue Paul Barruel", "France", "Paris"); aProvider.address = anAddress ; tx.commit(); // objects are stored into the data source, client cache is discarded, references are invalid tx.begin(); System.out.println(aProvider); // aProvider is refreshed from the data source tx.commit(); pm.close();
每次你修改了一个对象,它在JDO缓存中对应的实体将被标示为“脏(dirty)”。
例子 tx.begin(); aProvider.address = aNewAdr; // aProvider is marked as dirty tx.commit(); // aProvider will updated in the data source