public void test() { Session session = getSession(); UnitOfWork uow = session.acquireUnitOfWork(); uow.setShouldNewObjectsBeCached(true); Weather weather = new Weather(); weather.setStormPattern("Something really bad"); weather.id = 0; ReadObjectQuery query = new ReadObjectQuery(weather); query.checkCacheOnly(); Weather weatherClone = (Weather) uow.registerObject(weather); weather = (Weather) uow.executeQuery(query); zeroFailed = weather == null; Weather weatherNeg = new Weather(); weatherNeg.setStormPattern("Something really bad below zero"); weatherNeg.id = -1; ReadObjectQuery queryNeg = new ReadObjectQuery(weatherNeg); queryNeg.checkCacheOnly(); Weather weatherNegClone = (Weather) uow.registerObject(weatherNeg); weatherNeg = (Weather) uow.executeQuery(queryNeg); negativeFailed = weatherNeg == null; if (keepSequencing) { uow.assignSequenceNumbers(); zeroOverridden = weatherClone.id != 0; negativeOverridden = weatherNegClone.id != -1; } }
public void persistExample(Session session) { Vector allObjects = new Vector(); UnitOfWork unitOfWork = session.acquireUnitOfWork(); PopulationManager.getDefaultManager().addAllObjectsForClass(Person.class, allObjects); PopulationManager.getDefaultManager().addAllObjectsForClass(AAA.class, allObjects); PopulationManager.getDefaultManager().addAllObjectsForClass(Company.class, allObjects); PopulationManager.getDefaultManager().addAllObjectsForClass(Computer.class, allObjects); unitOfWork.registerAllObjects(allObjects); unitOfWork.commit(); }
/* * This test creates an object and registers it with a unit of work. It then serializes that * object and deserializes it. Adds an object onto the origional then performs serialization * sequence again. Then deepMergeClone is attempted and the results are compared to verify that * the merge worked. */ public void test() { try { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); ObjectOutputStream stream = new ObjectOutputStream(byteStream); // create the phoneNumber object Session session = getSession(); UnitOfWork uow = session.acquireUnitOfWork(); this.deptObject = (Dept) session.readObject( Dept.class, new org.eclipse.persistence.expressions.ExpressionBuilder() .get("deptno") .equal(5.0)); Dept deptClone = (Dept) uow.registerObject(this.deptObject); // force instantiations of value holders before serialization deptClone.getEmpCollection().size(); Emp empClone = null; if ((deptClone.getEmpCollection() != null) && (deptClone.getEmpCollection().size() > 1)) { empClone = (Emp) deptClone.getEmpCollection().iterator().next(); deptClone.getEmpCollection().remove(empClone); empClone.setDeptno(null); } // serialize object by writing to a stream stream.writeObject(deptClone); stream.writeObject(empClone); stream.flush(); byte[] arr = byteStream.toByteArray(); ByteArrayInputStream inByteStream = new ByteArrayInputStream(arr); ObjectInputStream inObjStream = new ObjectInputStream(inByteStream); Dept deserialDept; Emp deserialEmp = null; // deserialize the object try { deserialDept = (Dept) inObjStream.readObject(); deserialEmp = (Emp) inObjStream.readObject(); } catch (ClassNotFoundException e) { throw new TestErrorException("Could not deserialize object " + e.toString()); } // add a new manager, test 1-m's /* if (deserialDept.getEmpCollection() != null && deserialDept.getEmpCollection().size() > 1) { deserialEmp = (Emp)deserialDept.getEmpCollection().iterator().next(); deserialDept.getEmpCollection().remove(deserialEmp); deserialEmp.setDeptno(null); } */ deserialDept.getEmpCollection().add(deserialEmp); deserialEmp.setDeptno(deserialDept); int collectionSize = deserialDept.getEmpCollection().size(); byteStream = new ByteArrayOutputStream(); stream = new ObjectOutputStream(byteStream); // send the ammended object back through the serialization process stream.writeObject(deserialDept); stream.flush(); arr = byteStream.toByteArray(); inByteStream = new ByteArrayInputStream(arr); inObjStream = new ObjectInputStream(inByteStream); try { deserialDept = (Dept) inObjStream.readObject(); } catch (ClassNotFoundException e) { throw new TestErrorException("Could not deserialize object " + e.toString()); } // merge the ammended clone with the unit of work deptClone = (Dept) uow.deepMergeClone(deserialDept); if (deptClone.getEmpCollection().size() != collectionSize) { throw new TestErrorException("Failed to merge the collection correctly not enough Emps"); } for (Iterator iterator = deptClone.getEmpCollection().iterator(); iterator.hasNext(); ) { Emp emp = (Emp) iterator.next(); if (emp.getDeptno() != deptClone) { throw new TestErrorException("Failed to merge the back pointer"); } } uow.commit(); } catch (IOException e) { throw new TestErrorException("Error running Test " + e.toString()); } }
public void persistExample(Session session) { Vector allObjects = new Vector(); allObjects.add(example1()); allObjects.add(example2()); allObjects.add(example3()); allObjects.add(example4()); // Bug 387491 - Three JUnitJPQLDateTimeTestSuite tests fail with Oracle jdbc 12.1 driver // Starting with Oracle jdbc 12.1 Statement.setDate no longer truncates time component of // sql.Date. // The following code makes Oracle9Platform to do that by setting shouldTruncateDate flag to // "true". boolean hasSetTruncateDate = false; if (session.getPlatform().isOracle9()) { try { Class clazz = PrivilegedAccessHelper.getClassForName( "org.eclipse.persistence.platform.database.oracle.Oracle9Platform"); Method getDriverVersionMethod = PrivilegedAccessHelper.getMethod(clazz, "getDriverVersion", null, false); String driverVersion = (String) PrivilegedAccessHelper.invokeMethod( getDriverVersionMethod, session.getPlatform(), null); if (Helper.compareVersions(driverVersion, "12.1") >= 0) { Method shouldTruncateDateMethod = PrivilegedAccessHelper.getMethod(clazz, "shouldTruncateDate", null, false); boolean shouldTruncateDate = (Boolean) PrivilegedAccessHelper.invokeMethod( shouldTruncateDateMethod, session.getPlatform(), null); if (!shouldTruncateDate) { Method setShouldTruncateDateMethod = PrivilegedAccessHelper.getMethod( clazz, "setShouldTruncateDate", new Class[] {boolean.class}, false); PrivilegedAccessHelper.invokeMethod( setShouldTruncateDateMethod, session.getPlatform(), new Object[] {true}); hasSetTruncateDate = true; } } } catch (Exception ex) { throw new RuntimeException("Failed oracle9Platform.setShouldTruncateDate(true)", ex); } } UnitOfWork unitOfWork = session.acquireUnitOfWork(); unitOfWork.registerAllObjects(allObjects); unitOfWork.commit(); if (hasSetTruncateDate) { // Now setting shouldTruncateDate flag back to its original value "false". try { Class clazz = PrivilegedAccessHelper.getClassForName( "org.eclipse.persistence.platform.database.oracle.Oracle9Platform"); Method setShouldTruncateDateMethod = PrivilegedAccessHelper.getMethod( clazz, "setShouldTruncateDate", new Class[] {boolean.class}, false); PrivilegedAccessHelper.invokeMethod( setShouldTruncateDateMethod, session.getPlatform(), new Object[] {false}); } catch (Exception ex) { throw new RuntimeException("Failed oracle9Platform.setShouldTruncateDate(false)", ex); } } }