@Test
  @FailureExpected(jiraKey = "HHH-9239")
  public void testNestedUnidirOneToManyBackrefWithRemovedElement() {
    Item item1 = new Item();
    item1.setName("item1 name");
    SubItem subItem1 = new SubItem();
    subItem1.setName("subItem1 name");
    item1.getSubItemsBackref().add(subItem1);
    SubItem subItem2 = new SubItem();
    subItem2.setName("subItem2 name");
    item1.getSubItemsBackref().add(subItem2);

    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(item1);
    tx.commit();
    s.close();

    // get another representation of item1
    s = openSession();
    tx = s.beginTransaction();
    Item item1_1 = (Item) s.get(Item.class, item1.getId());
    Hibernate.initialize(item1_1.getSubItemsBackref());
    tx.commit();
    s.close();

    // remove subItem1 from the nested Item
    item1_1.getSubItemsBackref().remove(subItem1);

    Category category = new Category();
    category.setName("category");
    item1.setCategory(category);
    category.setExampleItem(item1_1);

    s = openSession();
    tx = s.beginTransaction();
    Item item1Merged = (Item) s.merge(item1);
    // the element should have been removed
    assertEquals(1, item1Merged.getSubItemsBackref().size());
    assertTrue(item1Merged.getSubItemsBackref().contains(subItem2));
    tx.commit();
    s.close();

    s = openSession();
    tx = s.beginTransaction();
    item1 = (Item) s.get(Item.class, item1.getId());
    assertEquals(1, item1.getSubItemsBackref().size());
    assertTrue(item1.getSubItemsBackref().contains(subItem2));
    // because cascade includes "delete-orphan" the removed SubItem should have been deleted.
    subItem1 = (SubItem) s.get(SubItem.class, subItem1.getId());
    assertNull(subItem1);
    tx.commit();
    s.close();

    cleanup();
  }
  @Test
  @FailureExpected(jiraKey = "HHH-9239")
  public void testNestedUnidirOneToManyBackrefWithNewElement() {
    Item item1 = new Item();
    item1.setName("item1 name");
    SubItem subItem1 = new SubItem();
    subItem1.setName("subItem1 name");
    item1.getSubItemsBackref().add(subItem1);

    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(item1);
    tx.commit();
    s.close();

    // get another representation of item1
    s = openSession();
    tx = s.beginTransaction();
    Item item1_1 = (Item) s.get(Item.class, item1.getId());
    Hibernate.initialize(item1_1.getSubItemsBackref());
    tx.commit();
    s.close();

    Category category = new Category();
    category.setName("category");
    item1.setCategory(category);

    // Add a new SubItem to the Item representation that will be in a nested association.
    SubItem subItem2 = new SubItem();
    subItem2.setName("subItem2 name");
    item1_1.getSubItemsBackref().add(subItem2);

    category.setExampleItem(item1_1);

    s = openSession();
    tx = s.beginTransaction();
    Item item1Merged = (Item) s.merge(item1);
    // The resulting collection should contain the added element
    assertEquals(2, item1Merged.getSubItemsBackref().size());
    assertEquals("subItem1 name", item1Merged.getSubItemsBackref().get(0).getName());
    assertEquals("subItem2 name", item1Merged.getSubItemsBackref().get(1).getName());
    tx.commit();
    s.close();

    s = openSession();
    tx = s.beginTransaction();
    item1 = (Item) s.get(Item.class, item1.getId());
    assertEquals(2, item1.getSubItemsBackref().size());
    assertEquals("subItem1 name", item1.getSubItemsBackref().get(0).getName());
    assertEquals("subItem2 name", item1.getSubItemsBackref().get(1).getName());
    tx.commit();
    s.close();

    cleanup();
  }
  @Test
  // @FailureExpected( jiraKey = "HHH-9106" )
  public void testTopLevelUnidirOneToManyBackrefWithRemovedElement() {
    Item item1 = new Item();
    item1.setName("item1 name");
    SubItem subItem1 = new SubItem();
    subItem1.setName("subItem1 name");
    item1.getSubItemsBackref().add(subItem1);
    SubItem subItem2 = new SubItem();
    subItem2.setName("subItem2 name");
    item1.getSubItemsBackref().add(subItem2);

    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(item1);
    tx.commit();
    s.close();

    // get another representation of item1
    s = openSession();
    tx = s.beginTransaction();
    Item item1_1 = (Item) s.get(Item.class, item1.getId());
    tx.commit();
    s.close();

    assertFalse(Hibernate.isInitialized(item1_1.getSubItemsBackref()));

    Category category = new Category();
    category.setName("category");

    item1.setCategory(category);
    category.setExampleItem(item1_1);

    // remove subItem1 from top-level Item
    item1.getSubItemsBackref().remove(subItem1);

    s = openSession();
    tx = s.beginTransaction();
    Item item1Merged = (Item) s.merge(item1);
    // entity should have been removed
    assertEquals(1, item1Merged.getSubItemsBackref().size());
    tx.commit();
    s.close();

    s = openSession();
    tx = s.beginTransaction();
    item1 = (Item) s.get(Item.class, item1.getId());
    assertEquals(1, item1.getSubItemsBackref().size());
    subItem1 = (SubItem) s.get(SubItem.class, subItem1.getId());
    // cascade does not include delete-orphan, so subItem1 should still be persistent.
    assertNotNull(subItem1);
    tx.commit();

    cleanup();
  }
  @Test
  @FailureExpected(jiraKey = "HHH-9240")
  public void testTopLevelUnidirOneToManyBackrefWithNewElement() {
    Item item1 = new Item();
    item1.setName("item1 name");
    SubItem subItem1 = new SubItem();
    subItem1.setName("subItem1 name");
    item1.getSubItemsBackref().add(subItem1);

    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(item1);
    tx.commit();
    s.close();

    // get another representation of item1
    s = openSession();
    tx = s.beginTransaction();
    Item item1_1 = (Item) s.get(Item.class, item1.getId());
    tx.commit();
    s.close();

    assertFalse(Hibernate.isInitialized(item1_1.getSubItemsBackref()));

    Category category = new Category();
    category.setName("category");

    SubItem subItem2 = new SubItem();
    subItem2.setName("subItem2 name");
    item1.getSubItemsBackref().add(subItem2);

    item1.setCategory(category);
    category.setExampleItem(item1_1);

    s = openSession();
    tx = s.beginTransaction();
    // The following will fail due to PropertyValueException because item1  will
    // be removed from the inverted merge map when the operation cascades to item1_1.
    Item item1Merged = (Item) s.merge(item1);
    // top-level collection should win
    assertEquals(2, item1.getSubItemsBackref().size());
    tx.commit();
    s.close();

    s = openSession();
    tx = s.beginTransaction();
    item1 = (Item) s.get(Item.class, item1.getId());
    assertEquals(2, item1.getSubItemsBackref().size());
    tx.commit();
    s.close();

    cleanup();
  }