Search in sources :

Example 1 with MergeContext

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

the class MergeContextTest method testMergeToManagedEntityFillFollowedByInvertUsingPutWithSetOperatedOnArg.

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

Example 2 with MergeContext

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

the class MergeContextTest method testManagedEntityAssociatedWithNewAndExistingMergeEntities.

@Test
public void testManagedEntityAssociatedWithNewAndExistingMergeEntities() {
    MergeContext cache = new MergeContext(session, new DoNothingEntityCopyObserver());
    session.getTransaction().begin();
    Simple mergeEntity = new Simple(1);
    Simple managedEntity = new Simple(0);
    cache.put(mergeEntity, managedEntity);
    cache.put(new Simple(1), managedEntity);
}
Also used : MergeContext(org.hibernate.event.spi.MergeContext) Test(org.junit.Test)

Example 3 with MergeContext

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

the class MergeContextTest method testMergeToManagedEntityFillFollowedByModifyEntrySet.

@Test
public void testMergeToManagedEntityFillFollowedByModifyEntrySet() {
    MergeContext cache = new MergeContext(session, new DoNothingEntityCopyObserver());
    Object mergeEntity = new Simple(1);
    Object managedEntity = new Simple(2);
    cache.put(mergeEntity, managedEntity, true);
    Iterator it = cache.entrySet().iterator();
    try {
        it.remove();
        fail("should have thrown UnsupportedOperationException");
    } catch (UnsupportedOperationException ex) {
    // expected
    }
    Map.Entry entry = (Map.Entry) cache.entrySet().iterator().next();
    try {
        cache.entrySet().remove(entry);
        fail("should have thrown UnsupportedOperationException");
    } catch (UnsupportedOperationException ex) {
    // expected
    }
    Map.Entry anotherEntry = new Map.Entry() {

        private Object key = new Simple(3);

        private Object value = 4;

        @Override
        public Object getKey() {
            return key;
        }

        @Override
        public Object getValue() {
            return value;
        }

        @Override
        public Object setValue(Object value) {
            Object oldValue = this.value;
            this.value = value;
            return oldValue;
        }
    };
    try {
        cache.entrySet().add(anotherEntry);
        fail("should have thrown UnsupportedOperationException");
    } catch (UnsupportedOperationException ex) {
    // expected
    }
}
Also used : Iterator(java.util.Iterator) MergeContext(org.hibernate.event.spi.MergeContext) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with MergeContext

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

the class MergeContextTest method testMergeToManagedEntityFillFollowedByInvert.

@Test
public void testMergeToManagedEntityFillFollowedByInvert() {
    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.invertMap().containsKey(managedEntity));
    assertFalse(cache.invertMap().containsKey(mergeEntity));
}
Also used : MergeContext(org.hibernate.event.spi.MergeContext) Test(org.junit.Test)

Example 5 with MergeContext

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

the class MergeContextTest method testMergeToManagedEntityFillFollowedByModifyValueOfEntrySetElement.

@Test
public void testMergeToManagedEntityFillFollowedByModifyValueOfEntrySetElement() {
    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.getValue()).setValue(2);
    assertEquals(2, managedEntity.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)

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