public void testMasterUpdate() throws PersistenceException { Complex fullname = new Complex("First", "Person"); // 1. load master object, add a new dependent object and commit _db.begin(); TestLazyPerson loadPerson = (TestLazyEmployee) _db.load(TestLazyEmployee.class, fullname); TestLazyAddress address = new TestLazyAddress(); address.setId(201); address.setStreet("Halfpipe 1"); address.setCity("Skater Heaven"); address.setPerson(loadPerson); loadPerson.getAddress().add(address); _db.commit(); // 2. reuse master and commit. if we get a DuplicateIdentityException, // the dependent object (id=201) was created twice. _db.begin(); _db.update(loadPerson); try { _db.commit(); } catch (TransactionAbortedException e) { if (e.getException() instanceof DuplicateIdentityException) { stream.println("Error: The dependent object Address was just duplicated"); fail("The dependent object Address was just duplicated"); } else { throw e; } } // No DuplicateIdentityException means, that just one instance of Address (id=201) // was created in the database. That's what we want to have. }
public void testDepends() throws Exception { Database db = null; MasterObjectNoKeyGen master = new MasterObjectNoKeyGen(); master.setId(100); master.setDescrip("This is the descrip."); db = _category.getDatabase(); db.begin(); db.create(master); db.commit(); assertTrue(master.getId() != 0); assertEquals(100, master.getId()); // THIS Part Works! db.begin(); DependentObjectNoKeyGen depends = new DependentObjectNoKeyGen(); depends.setId(101); depends.setDescrip("Description"); master.setDepends(depends); db.update(master); db.commit(); assertTrue(master.getId() != 0); assertEquals(100, master.getId()); int masterId = master.getId(); db.begin(); master = (MasterObjectNoKeyGen) db.load(MasterObjectNoKeyGen.class, new Integer(masterId)); assertNotNull(master.getDepends()); master.setDepends(null); db.commit(); db.begin(); master = (MasterObjectNoKeyGen) db.load(MasterObjectNoKeyGen.class, new Integer(masterId)); assertNull(master.getDepends()); db.commit(); // THIS part doesn't! db.begin(); master = (MasterObjectNoKeyGen) db.load(MasterObjectNoKeyGen.class, new Integer(masterId)); depends = new DependentObjectNoKeyGen(); depends.setId(102); depends.setDescrip("Description"); master.setDepends(depends); db.commit(); db.begin(); master = (MasterObjectNoKeyGen) db.load(MasterObjectNoKeyGen.class, new Integer(masterId)); assertNotNull(master.getDepends()); db.commit(); db.begin(); master = (MasterObjectNoKeyGen) db.load(MasterObjectNoKeyGen.class, new Integer(masterId)); assertNotNull(master); db.remove(master); db.commit(); }
public void runTest() throws PersistenceException { TimeStampableObject object; CallbacksInvoked cbi = new CallbacksInvoked(); cbi.allow(CallbacksInvoked.CREATING); cbi.allow(CallbacksInvoked.USING); cbi.allow(CallbacksInvoked.CREATED); cbi.allow(CallbacksInvoked.STORING); cbi.allow(CallbacksInvoked.RELEASING); _i._callbacksInvoked.init(); _db.begin(); object = new TimeStampableObject(); LOG.debug("Creating new object: " + object); _db.create(object); _db.commit(); if (!cbi.equals(_i._callbacksInvoked)) { LOG.error("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked); fail("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked); } cbi.init(); cbi.allow(CallbacksInvoked.USING); cbi.allow(CallbacksInvoked.LOADED); cbi.allow(CallbacksInvoked.STORING); cbi.allow(CallbacksInvoked.RELEASING); cbi.allow(CallbacksInvoked.INSTANTIATE); _i._callbacksInvoked.init(); _db.begin(); object = (TimeStampableObject) _db.load(TimeStampableObject.class, new Integer(TimeStampableObject.DEFAULT_ID)); object.setValue1("Alan"); _db.commit(); if (!cbi.equals(_i._callbacksInvoked)) { LOG.error("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked); fail("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked); } cbi.init(); cbi.allow(CallbacksInvoked.USING); cbi.allow(CallbacksInvoked.UPDATED); cbi.allow(CallbacksInvoked.STORING); cbi.allow(CallbacksInvoked.RELEASING); _i._callbacksInvoked.init(); object.setValue2("long transaction new value"); _db.begin(); _db.update(object); _db.commit(); if (!cbi.equals(_i._callbacksInvoked)) { LOG.error("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked); fail("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked); } cbi.init(); cbi.allow(CallbacksInvoked.USING); cbi.allow(CallbacksInvoked.LOADED); cbi.allow(CallbacksInvoked.REMOVING); cbi.allow(CallbacksInvoked.REMOVED); cbi.allow(CallbacksInvoked.RELEASING); cbi.allow(CallbacksInvoked.INSTANTIATE); _i._callbacksInvoked.init(); _db.begin(); object = (TimeStampableObject) _db.load(TimeStampableObject.class, new Integer(TimeStampableObject.DEFAULT_ID)); _db.remove(object); _db.commit(); if (!cbi.equals(_i._callbacksInvoked)) { LOG.error("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked); fail("Callbacks were not properly invoked: " + cbi + " != " + _i._callbacksInvoked); } }
public void testLongTransaction() throws Exception { Product product = null; ProductGroup group; QueryResults results; Database db = getJDOManager(DBNAME, MAPPING).getDatabase(); db.begin(); LOG.info("Begin transaction to remove Product objects"); // Look up the products and if found delete them from the database OQLQuery productOql = db.getOQLQuery("SELECT p FROM " + Product.class.getName() + " p WHERE p.id = $1"); for (int i = 4; i < 10; ++i) { productOql.bind(i); results = productOql.execute(); while (results.hasMore()) { product = (Product) results.next(); LOG.debug("Deleting existing product: " + product); db.remove(product); } } LOG.info("End transaction to remove Product objects"); db.commit(); db.begin(); LOG.info("Begin transaction: one-to-one, one-to-many, and dependent relations"); // If no such group exists in the database, create a new object and persist it OQLQuery groupOql = db.getOQLQuery("SELECT g FROM " + ProductGroup.class.getName() + " g WHERE id = $1"); groupOql.bind(3); results = groupOql.execute(); if (!results.hasMore()) { group = new ProductGroup(); group.setId(3); group.setName("a group"); db.create(group); LOG.debug("Creating new group: " + group); } else { group = (ProductGroup) results.next(); LOG.debug("Query result: " + group); } // If no such product exists in the database, create a new object and persist it // Note: product uses group, so group object has to be created first, but can // be persisted later productOql.bind(4); results = productOql.execute(); if (!results.hasMore()) { product = new Product(); product.setId(4); product.setName("product4"); product.setPrice(200); product.setGroup(group); LOG.debug("Creating new product: " + product); db.create(product); } else { LOG.debug("Query result: " + results.next()); } LOG.info("End transaction: one-to-one, one-to-many and dependent relations"); db.commit(); db.begin(); LOG.info("Begin transaction: one-to-one and dependent relations"); group = (ProductGroup) db.load(ProductGroup.class, new Integer(3)); // If no such products with ids 5-8 exist, create new objects and persist them for (int i = 5; i < 10; ++i) { int j = i + 1; productOql.bind(j); results = productOql.execute(); if (!results.hasMore()) { product = new Product(); product.setId(i); product.setName("product" + product.getId()); product.setPrice(300); product.setGroup(group); LOG.debug("Creating new product: " + product); db.create(product); } else { LOG.debug("Query result: " + results.next()); } } LOG.info("End transaction: one-to-one and dependent relations"); db.commit(); product.setPrice(333); LOG.info("Updated Product price: " + product); db.begin(); LOG.info("Begin transaction: long transaction"); // Don't forget to implement TimeStampable for the long transaction!!! db.update(product); LOG.info("End transaction: long transaction"); db.commit(); db.close(); LOG.info("Test complete"); }