Hi, I am new to JDO and trying to get the PersistenceManager.detachCopy functionality working. I am using ObjectDB 2.8.3
I am
1-saving an object to the db
2-loading that object from the db
3-creating a detached copy
4-closing the connection
5-modifying the detached copy
6-re-opening the connection
7-saving the detached copy
I get an "Attempt to reuse an existing primary key value" error. I have tried enhancing the jar, but did run into some trouble with this as well. Not sure whether enhancement is required?
My code:
@Entity public class Boat implements Serializable{ @Id private String name; private int age; public Boat(String name, int age){ this.name=name; this.age=age; } (getters, setters) } ... Boat b=null; Boat b_copy=null; try { Properties properties = new Properties(); properties.setProperty("javax.jdo.PersistenceManagerFactoryClass", "com.objectdb.jdo.PMF"); properties.setProperty("javax.jdo.option.ConnectionURL", "objectdbtest2_db.odb"); PersistenceManagerFactory pmf =JDOHelper.getPersistenceManagerFactory(properties); PersistenceManager pm = pmf.getPersistenceManager(); try { pm.currentTransaction().begin(); Query q=pm.newQuery(Boat.class); Collection res=(Collection)q.execute(); b=(Boat)res.iterator().next(); b_copy=pm.detachCopy(b); pm.currentTransaction().commit(); }finally { if (pm.currentTransaction().isActive()) pm.currentTransaction().rollback(); if (!pm.isClosed()) pm.close(); if (!pmf.isClosed())pmf.close(); } }catch (RuntimeException x) { System.err.println("Error: " + x.getMessage()); } b_copy.setAge(51); // modification! try { Properties properties = new Properties(); properties.setProperty("javax.jdo.PersistenceManagerFactoryClass", "com.objectdb.jdo.PMF"); properties.setProperty("javax.jdo.option.ConnectionURL", "objectdbtest2_db.odb"); PersistenceManagerFactory pmf =JDOHelper.getPersistenceManagerFactory(properties); PersistenceManager pm = pmf.getPersistenceManager(); try { pm.currentTransaction().begin(); pm.makePersistent(b_copy); pm.currentTransaction().commit(); }finally { if (pm.currentTransaction().isActive()) pm.currentTransaction().rollback(); if (!pm.isClosed()) pm.close(); if (!pmf.isClosed())pmf.close(); } }catch (RuntimeException x) { System.err.println("Error: " + x.getMessage()); } }
Thanks for any help!