コード例 #1
0
  protected void setup() {
    // save current null values for later restoration
    saveDefaultDefaultNullValues = ConversionManager.getDefaultManager().getDefaultNullValues();
    saveDefaultNullValues =
        getSession().getLogin().getPlatform().getConversionManager().getDefaultNullValues();
    getSession()
        .getLogin()
        .getPlatform()
        .getConversionManager()
        .setDefaultNullValues(new Hashtable());
    getSession().getLogin().setDefaultNullValue(String.class, "null");
    getSession().getLogin().setDefaultNullValue(int.class, new Integer(-1));
    // Reinit mappings.
    for (DatabaseMapping mapping : getSession().getDescriptor(Address.class).getMappings()) {
      if (mapping.isDirectToFieldMapping()) {
        mapping.preInitialize(getAbstractSession());
      }
    }
    getAbstractSession().beginTransaction();

    employee = new Employee();
    employee.setFirstName("Fred");
    employee.setLastName("Flintstone");
    employee.setSalary(22);
    employee.setGender("Male");
    Address address = new Address();
    address.setCity(null);
    employee.setAddress(address);

    getAbstractSession().writeObject(employee);
    // force the salary to be NULL
    getSession()
        .executeNonSelectingCall(
            new SQLCall("update SALARY set SALARY = null where EMP_ID = " + employee.getId()));
  }
    long getVersion(Employee emp) {
      Vector pk = new Vector(1);
      pk.add(emp.getId());

      return ((Long)
              getSession()
                  .getDescriptor(Employee.class)
                  .getOptimisticLockingPolicy()
                  .getWriteLockValue(emp, pk, getAbstractSession()))
          .longValue();
    }
  private void setArgumentsForTestUsing(Vector employees) {
    setArguments(new Vector());

    Enumeration names = getExpressionParameters().elements();
    Enumeration employeeEnum = employees.elements();
    while (names.hasMoreElements()) {
      Employee emp = (Employee) employeeEnum.nextElement();
      getArguments().add(emp.getId());
      names.nextElement();
    }
  }
    public void test() {
      // setup
      Employee manager = new Employee();
      manager.setFirstName("Manager");
      Employee employee = new Employee();
      employee.setFirstName("Employee");

      UnitOfWork uow = getSession().acquireUnitOfWork();
      uow.registerObject(manager);
      uow.registerObject(employee);
      uow.commit();

      Vector pk = new Vector(1);
      pk.add(manager.getId());

      version[0] =
          ((Long)
                  getSession()
                      .getDescriptor(Employee.class)
                      .getOptimisticLockingPolicy()
                      .getWriteLockValue(manager, pk, getAbstractSession()))
              .longValue();

      // test1 - add managed employee, manager's version should increment.
      uow = getSession().acquireUnitOfWork();
      Employee managerClone = (Employee) uow.registerObject(manager);
      Employee employeeClone = (Employee) uow.registerObject(employee);
      managerClone.addManagedEmployee(employeeClone);
      uow.commit();
      version[1] =
          ((Long)
                  getSession()
                      .getDescriptor(Employee.class)
                      .getOptimisticLockingPolicy()
                      .getWriteLockValue(manager, pk, getAbstractSession()))
              .longValue();

      // test2 - alter managed employee, manager's version should NOT increment.
      uow = getSession().acquireUnitOfWork();
      employeeClone = (Employee) uow.registerObject(employee);
      employeeClone.setFirstName("Altered");
      uow.commit();
      version[2] =
          ((Long)
                  getSession()
                      .getDescriptor(Employee.class)
                      .getOptimisticLockingPolicy()
                      .getWriteLockValue(manager, pk, getAbstractSession()))
              .longValue();

      // test3- remove managed employee, manager's version should increment.
      uow = getSession().acquireUnitOfWork();
      managerClone = (Employee) uow.registerObject(manager);
      employeeClone = (Employee) uow.registerObject(employee);
      managerClone.removeManagedEmployee(employeeClone);
      uow.commit();
      version[3] =
          ((Long)
                  getSession()
                      .getDescriptor(Employee.class)
                      .getOptimisticLockingPolicy()
                      .getWriteLockValue(manager, pk, getAbstractSession()))
              .longValue();

      PhoneNumber phone = new PhoneNumber("home", "613", "1111111");

      // test4 - add phone, manager's version should increment.
      uow = getSession().acquireUnitOfWork();
      managerClone = (Employee) uow.registerObject(manager);
      PhoneNumber phoneClone = (PhoneNumber) uow.registerObject(phone);
      managerClone.addPhoneNumber(phoneClone);
      uow.commit();
      version[4] =
          ((Long)
                  getSession()
                      .getDescriptor(Employee.class)
                      .getOptimisticLockingPolicy()
                      .getWriteLockValue(manager, pk, getAbstractSession()))
              .longValue();

      // test5- alter phone, manager's version should increment.
      uow = getSession().acquireUnitOfWork();
      phoneClone = (PhoneNumber) uow.registerObject(phone);
      phoneClone.setType("work");
      uow.commit();
      version[5] =
          ((Long)
                  getSession()
                      .getDescriptor(Employee.class)
                      .getOptimisticLockingPolicy()
                      .getWriteLockValue(manager, pk, getAbstractSession()))
              .longValue();

      // test6- remove phone, manager's version should increment.
      uow = getSession().acquireUnitOfWork();
      managerClone = (Employee) uow.registerObject(manager);
      phoneClone = (PhoneNumber) uow.registerObject(phone);
      managerClone.removePhoneNumber(phoneClone);
      uow.commit();
      version[6] =
          ((Long)
                  getSession()
                      .getDescriptor(Employee.class)
                      .getOptimisticLockingPolicy()
                      .getWriteLockValue(manager, pk, getAbstractSession()))
              .longValue();
    }