Search in sources :

Example 21 with FailureExpected

use of org.hibernate.testing.FailureExpected in project hibernate-orm by hibernate.

the class MergeMultipleEntityCopiesAllowedOrphanDeleteTest method testNestedUnidirOneToManyBackrefWithNewElement.

@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();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test) FailureExpected(org.hibernate.testing.FailureExpected)

Example 22 with FailureExpected

use of org.hibernate.testing.FailureExpected in project hibernate-orm by hibernate.

the class MergeMultipleEntityCopiesAllowedTest method testTopLevelUnidirOneToManyBackrefWithNewElement.

@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();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test) FailureExpected(org.hibernate.testing.FailureExpected)

Example 23 with FailureExpected

use of org.hibernate.testing.FailureExpected in project hibernate-orm by hibernate.

the class MergeMultipleEntityCopiesAllowedTest method testNestedUnidirOneToManyNoBackrefWithNewElement.

@Test
@FailureExpected(jiraKey = "HHH-9239")
public void testNestedUnidirOneToManyNoBackrefWithNewElement() {
    Category category1 = new Category();
    category1.setName("category1 name");
    SubCategory subCategory1 = new SubCategory();
    subCategory1.setName("subCategory1 name");
    category1.getSubCategories().add(subCategory1);
    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();
    SubCategory subCategory2 = new SubCategory();
    subCategory2.setName("subCategory2 name");
    category1_1.getSubCategories().add(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(2, category1Merged.getSubCategories().size());
    tx.commit();
    s.close();
    s = openSession();
    tx = s.beginTransaction();
    category1 = (Category) s.get(Category.class, category1.getId());
    assertEquals(2, category1.getSubCategories().size());
    tx.commit();
    s.close();
    cleanup();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test) FailureExpected(org.hibernate.testing.FailureExpected)

Example 24 with FailureExpected

use of org.hibernate.testing.FailureExpected in project hibernate-orm by hibernate.

the class MergeMultipleEntityCopiesAllowedTest method testNestedUnidirOneToManyNoBackrefWithRemovedElement.

@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));
    // 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();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test) FailureExpected(org.hibernate.testing.FailureExpected)

Example 25 with FailureExpected

use of org.hibernate.testing.FailureExpected in project hibernate-orm by hibernate.

the class MergeMultipleEntityCopiesAllowedTest method testNestedUnidirOneToManyBackrefWithRemovedElement.

@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);
    // 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();
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test) FailureExpected(org.hibernate.testing.FailureExpected)

Aggregations

FailureExpected (org.hibernate.testing.FailureExpected)59 Test (org.junit.Test)58 Session (org.hibernate.Session)39 Transaction (org.hibernate.Transaction)23 TestForIssue (org.hibernate.testing.TestForIssue)14 EntityManager (javax.persistence.EntityManager)12 List (java.util.List)7 ArrayList (java.util.ArrayList)4 Date (java.util.Date)3 MetadataSources (org.hibernate.boot.MetadataSources)3 RequiresDialect (org.hibernate.testing.RequiresDialect)3 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Callable (java.util.concurrent.Callable)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 EntityManagerFactory (javax.persistence.EntityManagerFactory)2 Query (javax.persistence.Query)2