public final void validate(T object) { ObjectContext workDc = object.getObjectContext(); // .createChildDataContext(); T obj = (T) workDc.localObject(object.getObjectId(), object); Set<DataObject> toDelete = new HashSet<DataObject>(); try { if (!fakes.isEmpty()) { // Simulation des fakes for (String prop : fakes.keySet()) { if (obj.readProperty(prop) != null) continue; DataObject value = (DataObject) workDc.newObject(fakes.get(prop)); obj.setToOneTarget(prop, value, false); toDelete.add(value); } } // Validation validateImpl(obj); } finally { // Annulation des modifs if (!fakes.isEmpty()) { // Suppression des fakes for (String prop : fakes.keySet()) { DataObject value = (DataObject) obj.readProperty(prop); if (!toDelete.contains(value)) continue; workDc.deleteObject(value); } } } // childDc.rollbackChangesLocally(); }
public static void main(String[] args) { ServerRuntime cayenneRuntime = new ServerRuntime("cayenne-CESDomain.xml", new CayenneExtrasModule()); // ServerRuntime cayenneRuntime = new ServerRuntime("cayenne-CESDomain.xml"); ObjectContext context = cayenneRuntime.getContext(); LineItem li = context.newObject(LineItem.class); Item i = context.newObject(Item.class); CostElement ce1 = context.newObject(CostElement.class); CostElement ce2 = context.newObject(CostElement.class); CostElement ce3 = context.newObject(CostElement.class); BudgetYear by1 = context.newObject(BudgetYear.class); BudgetYear by2 = context.newObject(BudgetYear.class); BudgetYear by3 = context.newObject(BudgetYear.class); li.addToItems(i); i.addToCostElements(ce1); ce1.addToCostElements(ce2); ce2.addToCostElements(ce3); ce3.setQuantity(by1); ce3.setTotalCost(by2); ce3.setUnitCost(by3); by1.setLineItem(li); by2.setLineItem(li); by3.setLineItem(li); // ce2.setItem(i); // ce3.setItem(i); context.commitChanges(); context.deleteObject(li); context.commitChanges(); }