Search in sources :

Example 6 with MergeContext

use of org.hibernate.event.spi.MergeContext in project hibernate-orm by hibernate.

the class MergeContextTest method testManagedAssociatedWith2ExistingMergeEntities.

@Test
public void testManagedAssociatedWith2ExistingMergeEntities() {
    MergeContext cache = new MergeContext(session, new DoNothingEntityCopyObserver());
    session.getTransaction().begin();
    Simple mergeEntity1 = new Simple(1);
    session.persist(mergeEntity1);
    Simple managedEntity1 = new Simple(1);
    cache.put(mergeEntity1, managedEntity1);
    Simple managedEntity2 = new Simple(2);
    try {
        cache.put(mergeEntity1, managedEntity2);
        fail("should have thrown IllegalArgumentException");
    } catch (IllegalArgumentException ex) {
    // expected; cannot change managed entity associated with a merge entity
    } finally {
        session.getTransaction().rollback();
    }
}
Also used : MergeContext(org.hibernate.event.spi.MergeContext) Test(org.junit.Test)

Example 7 with MergeContext

use of org.hibernate.event.spi.MergeContext in project hibernate-orm by hibernate.

the class DefaultMergeEventListener method onMerge.

/**
 * Handle the given merge event.
 *
 * @param event The merge event to be handled.
 */
public void onMerge(MergeEvent event) throws HibernateException {
    final EntityCopyObserver entityCopyObserver = createEntityCopyObserver(event.getSession().getFactory());
    final MergeContext mergeContext = new MergeContext(event.getSession(), entityCopyObserver);
    try {
        onMerge(event, mergeContext);
        entityCopyObserver.topLevelMergeComplete(event.getSession());
    } finally {
        entityCopyObserver.clear();
        mergeContext.clear();
    }
}
Also used : EntityCopyObserver(org.hibernate.event.spi.EntityCopyObserver) MergeContext(org.hibernate.event.spi.MergeContext)

Example 8 with MergeContext

use of org.hibernate.event.spi.MergeContext in project hibernate-orm by hibernate.

the class MergeContextTest method testMergeToManagedEntityFillFollowedByModifyKeyOfEntrySetElement.

@Test
public void testMergeToManagedEntityFillFollowedByModifyKeyOfEntrySetElement() {
    MergeContext cache = new MergeContext(session, new DoNothingEntityCopyObserver());
    Simple mergeEntity = new Simple(1);
    Simple managedEntity = new Simple(0);
    cache.put(mergeEntity, managedEntity, true);
    Map.Entry entry = (Map.Entry) cache.entrySet().iterator().next();
    ((Simple) entry.getKey()).setValue(2);
    assertEquals(2, mergeEntity.getValue());
    checkCacheConsistency(cache, 1);
    entry = (Map.Entry) cache.entrySet().iterator().next();
    assertSame(mergeEntity, entry.getKey());
    assertSame(managedEntity, entry.getValue());
}
Also used : MergeContext(org.hibernate.event.spi.MergeContext) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 9 with MergeContext

use of org.hibernate.event.spi.MergeContext in project hibernate-orm by hibernate.

the class MergeContextTest method testMergeToManagedEntityFillFollowedByInvertUsingPutAll.

@Test
public void testMergeToManagedEntityFillFollowedByInvertUsingPutAll() {
    MergeContext cache = new MergeContext(session, new DoNothingEntityCopyObserver());
    Map<Object, Object> input = new HashMap<Object, Object>();
    Object mergeEntity1 = new Simple(1);
    // 
    Object managedEntity1 = 1;
    input.put(mergeEntity1, managedEntity1);
    Object mergeEntity2 = new Simple(3);
    Object managedEntity2 = 2;
    input.put(mergeEntity2, managedEntity2);
    cache.putAll(input);
    checkCacheConsistency(cache, 2);
    assertTrue(cache.containsKey(mergeEntity1));
    assertFalse(cache.containsKey(managedEntity1));
    assertTrue(cache.containsKey(mergeEntity2));
    assertFalse(cache.containsKey(managedEntity2));
    assertTrue(cache.invertMap().containsKey(managedEntity1));
    assertFalse(cache.invertMap().containsKey(mergeEntity1));
    assertTrue(cache.invertMap().containsKey(managedEntity2));
    assertFalse(cache.invertMap().containsKey(mergeEntity2));
}
Also used : HashMap(java.util.HashMap) MergeContext(org.hibernate.event.spi.MergeContext) Test(org.junit.Test)

Example 10 with MergeContext

use of org.hibernate.event.spi.MergeContext in project hibernate-orm by hibernate.

the class MergeContextTest method testMergeToManagedEntityFillFollowedByInvertMapping.

@Test
public void testMergeToManagedEntityFillFollowedByInvertMapping() {
    MergeContext cache = new MergeContext(session, new DoNothingEntityCopyObserver());
    Object mergeEntity = new Simple(1);
    Object managedEntity = new Simple(2);
    cache.put(mergeEntity, managedEntity);
    checkCacheConsistency(cache, 1);
    assertTrue(cache.containsKey(mergeEntity));
    assertFalse(cache.containsKey(managedEntity));
    assertTrue(cache.containsValue(managedEntity));
    assertTrue(cache.invertMap().containsKey(managedEntity));
    assertFalse(cache.invertMap().containsKey(mergeEntity));
    assertTrue(cache.invertMap().containsValue(mergeEntity));
    cache.clear();
    checkCacheConsistency(cache, 0);
    assertFalse(cache.containsKey(mergeEntity));
    assertFalse(cache.invertMap().containsKey(managedEntity));
}
Also used : MergeContext(org.hibernate.event.spi.MergeContext) Test(org.junit.Test)

Aggregations

MergeContext (org.hibernate.event.spi.MergeContext)14 Test (org.junit.Test)13 HashMap (java.util.HashMap)5 Iterator (java.util.Iterator)4 Map (java.util.Map)4 EntityCopyObserver (org.hibernate.event.spi.EntityCopyObserver)1