Example #1
0
  /**
   * Check if the property is initialized. If the named property does not exist or is not
   * persistent, this method always returns <tt>true</tt>.
   *
   * @param proxy The potential proxy
   * @param propertyName the name of a persistent attribute of the object
   * @return true if the named property of the object is not listed as uninitialized; false
   *     otherwise
   */
  public static boolean isPropertyInitialized(Object proxy, String propertyName) {

    Object entity;
    if (proxy instanceof HibernateProxy) {
      LazyInitializer li = ((HibernateProxy) proxy).getHibernateLazyInitializer();
      if (li.isUninitialized()) {
        return false;
      } else {
        entity = li.getImplementation();
      }
    } else {
      entity = proxy;
    }

    if (FieldInterceptionHelper.isInstrumented(entity)) {
      FieldInterceptor interceptor = FieldInterceptionHelper.extractFieldInterceptor(entity);
      return interceptor == null || interceptor.isInitialized(propertyName);
    } else {
      return true;
    }
  }
 private boolean isUpdateNecessary(final FlushEntityEvent event, final boolean mightBeDirty) {
   final Status status = event.getEntityEntry().getStatus();
   if (mightBeDirty || status == Status.DELETED) {
     // compare to cached state (ignoring collections unless versioned)
     dirtyCheck(event);
     if (isUpdateNecessary(event)) {
       return true;
     } else {
       FieldInterceptionHelper.clearDirty(event.getEntity());
       return false;
     }
   } else {
     return hasDirtyCollections(event, event.getEntityEntry().getPersister(), status);
   }
 }
  @Test
  @SuppressWarnings({"UnusedAssignment", "UnnecessaryBoxing"})
  public void testSaveOrUpdateGot() throws Exception {
    boolean instrumented = FieldInterceptionHelper.isInstrumented(new NumberedNode());

    clearCounts();

    TestingJtaBootstrap.INSTANCE.getTransactionManager().begin();
    Session s = openSession();
    NumberedNode root = new NumberedNode("root");
    s.saveOrUpdate(root);
    s = applyNonFlushedChangesToNewSessionCloseOldSession(s);
    root = (NumberedNode) getOldToNewEntityRefMap().get(root);
    TestingJtaBootstrap.INSTANCE.getTransactionManager().commit();

    assertInsertCount(1);
    assertUpdateCount(0);
    clearCounts();

    TestingJtaBootstrap.INSTANCE.getTransactionManager().begin();
    s = openSession();
    s.saveOrUpdate(root);
    s = applyNonFlushedChangesToNewSessionCloseOldSession(s);
    root = (NumberedNode) getOldToNewEntityRefMap().get(root);
    TestingJtaBootstrap.INSTANCE.getTransactionManager().commit();

    assertInsertCount(0);
    assertUpdateCount(instrumented ? 0 : 1);

    TestingJtaBootstrap.INSTANCE.getTransactionManager().begin();
    s = openSession();
    root = (NumberedNode) s.get(NumberedNode.class, Long.valueOf(root.getId()));
    s = applyNonFlushedChangesToNewSessionCloseOldSession(s);
    root = (NumberedNode) getOldToNewEntityRefMap().get(root);
    Hibernate.initialize(root.getChildren());
    s = applyNonFlushedChangesToNewSessionCloseOldSession(s);
    root = (NumberedNode) getOldToNewEntityRefMap().get(root);
    TestingJtaBootstrap.INSTANCE.getTransactionManager().commit();

    clearCounts();

    TestingJtaBootstrap.INSTANCE.getTransactionManager().begin();
    s = openSession();
    NumberedNode child = new NumberedNode("child");
    root.addChild(child);
    s.saveOrUpdate(root);
    s = applyNonFlushedChangesToNewSessionCloseOldSession(s);
    root = (NumberedNode) getOldToNewEntityRefMap().get(root);
    assertTrue(Hibernate.isInitialized(root.getChildren()));
    child = (NumberedNode) root.getChildren().iterator().next();
    assertTrue(s.contains(child));
    TestingJtaBootstrap.INSTANCE.getTransactionManager().commit();

    assertInsertCount(1);
    assertUpdateCount(instrumented ? 0 : 1);

    TestingJtaBootstrap.INSTANCE.getTransactionManager().begin();
    s = openSession();
    assertEquals(
        s.createCriteria(NumberedNode.class).setProjection(Projections.rowCount()).uniqueResult(),
        new Long(2));
    s.delete(root);
    s.delete(child);
    TestingJtaBootstrap.INSTANCE.getTransactionManager().commit();
  }
  @Test
  @SuppressWarnings({"UnusedAssignment"})
  public void testSaveOrUpdateDeepTreeWithGeneratedId() throws Exception {
    boolean instrumented = FieldInterceptionHelper.isInstrumented(new NumberedNode());
    clearCounts();

    TestingJtaBootstrap.INSTANCE.getTransactionManager().begin();
    Session s = openSession();
    NumberedNode root = new NumberedNode("root");
    NumberedNode child = new NumberedNode("child");
    NumberedNode grandchild = new NumberedNode("grandchild");
    root.addChild(child);
    child.addChild(grandchild);
    s.saveOrUpdate(root);
    s = applyNonFlushedChangesToNewSessionCloseOldSession(s);
    root = (NumberedNode) getOldToNewEntityRefMap().get(root);
    child = (NumberedNode) getOldToNewEntityRefMap().get(child);
    grandchild = (NumberedNode) getOldToNewEntityRefMap().get(grandchild);
    TestingJtaBootstrap.INSTANCE.getTransactionManager().commit();

    assertInsertCount(3);
    assertUpdateCount(0);
    clearCounts();

    child = (NumberedNode) root.getChildren().iterator().next();
    grandchild = (NumberedNode) child.getChildren().iterator().next();
    grandchild.setDescription("the grand child");
    NumberedNode grandchild2 = new NumberedNode("grandchild2");
    child.addChild(grandchild2);

    TestingJtaBootstrap.INSTANCE.getTransactionManager().begin();
    s = openSession();
    s.saveOrUpdate(root);
    s = applyNonFlushedChangesToNewSessionCloseOldSession(s);
    root = (NumberedNode) getOldToNewEntityRefMap().get(root);
    TestingJtaBootstrap.INSTANCE.getTransactionManager().commit();

    assertInsertCount(1);
    assertUpdateCount(instrumented ? 1 : 3);
    clearCounts();

    NumberedNode child2 = new NumberedNode("child2");
    NumberedNode grandchild3 = new NumberedNode("grandchild3");
    child2.addChild(grandchild3);
    root.addChild(child2);

    TestingJtaBootstrap.INSTANCE.getTransactionManager().begin();
    s = openSession();
    s.saveOrUpdate(root);
    s = applyNonFlushedChangesToNewSessionCloseOldSession(s);
    TestingJtaBootstrap.INSTANCE.getTransactionManager().commit();

    assertInsertCount(2);
    assertUpdateCount(instrumented ? 0 : 4);
    clearCounts();

    TestingJtaBootstrap.INSTANCE.getTransactionManager().begin();
    s = openSession();
    s.createQuery("delete from NumberedNode where name like 'grand%'").executeUpdate();
    s.createQuery("delete from NumberedNode where name like 'child%'").executeUpdate();
    s.createQuery("delete from NumberedNode").executeUpdate();
    TestingJtaBootstrap.INSTANCE.getTransactionManager().commit();
  }