@Test // @FailureExpected( jiraKey = "HHH-9106" ) public void testTopLevelUnidirOneToManyNoBackrefWithRemovedElement() { Category category1 = new Category(); category1.setName("category1 name"); SubCategory subCategory1 = new SubCategory(); subCategory1.setName("subCategory1 name"); category1.getSubCategories().add(subCategory1); SubCategory subCategory2 = new SubCategory(); subCategory2.setName("subCategory2 name"); category1.getSubCategories().add(subCategory2); Session s = openSession(); Transaction tx = s.beginTransaction(); s.persist(category1); tx.commit(); s.close(); // get another representation of category1 s = openSession(); tx = s.beginTransaction(); Category category1_1 = (Category) s.get(Category.class, category1.getId()); tx.commit(); s.close(); assertFalse(Hibernate.isInitialized(category1_1.getSubCategories())); Item item = new Item(); item.setName("item"); category1.setExampleItem(item); item.setCategory(category1_1); category1.getSubCategories().remove(subCategory1); s = openSession(); tx = s.beginTransaction(); Category category1Merged = (Category) s.merge(category1); assertEquals(1, category1Merged.getSubCategories().size()); assertTrue(category1Merged.getSubCategories().contains(subCategory2)); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); category1 = (Category) s.get(Category.class, category1.getId()); assertEquals(1, category1.getSubCategories().size()); assertTrue(category1.getSubCategories().contains(subCategory2)); // cascade does not include delete-orphan, so subCategory1 should still be persistent. subCategory1 = (SubCategory) s.get(SubCategory.class, subCategory1.getId()); assertNotNull(subCategory1); tx.commit(); s.close(); cleanup(); }
@Test @FailureExpected(jiraKey = "HHH-9239") public void testNestedUnidirOneToManyNoBackrefWithRemovedElement() { Category category1 = new Category(); category1.setName("category1 name"); SubCategory subCategory1 = new SubCategory(); subCategory1.setName("subCategory1 name"); category1.getSubCategories().add(subCategory1); SubCategory subCategory2 = new SubCategory(); subCategory2.setName("subCategory2 name"); category1.getSubCategories().add(subCategory2); Session s = openSession(); Transaction tx = s.beginTransaction(); s.persist(category1); tx.commit(); s.close(); // get another representation of category1 s = openSession(); tx = s.beginTransaction(); Category category1_1 = (Category) s.get(Category.class, category1.getId()); Hibernate.initialize(category1_1.getSubCategories()); tx.commit(); s.close(); category1_1.getSubCategories().remove(subCategory2); Item item = new Item(); item.setName("item"); category1.setExampleItem(item); item.setCategory(category1_1); s = openSession(); tx = s.beginTransaction(); Category category1Merged = (Category) s.merge(category1); assertEquals(1, category1Merged.getSubCategories().size()); assertTrue(category1Merged.getSubCategories().contains(subCategory2)); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); category1 = (Category) s.get(Category.class, category1.getId()); assertEquals(1, category1.getSubCategories().size()); assertTrue(category1.getSubCategories().contains(subCategory2)); subCategory1 = (SubCategory) s.get(SubCategory.class, subCategory1.getId()); assertNull(subCategory1); tx.commit(); s.close(); cleanup(); }