Skip to content

Commit

Permalink
HHH-9028 corrected Session#get for caching w/ polymorphism, improved
Browse files Browse the repository at this point in the history
tests
  • Loading branch information
brmeyer committed Mar 7, 2014
1 parent 04c57c8 commit 768d02d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ protected Object loadFromSecondLevelCache(
final CacheKey ck = source.generateCacheKey(
event.getEntityId(),
persister.getIdentifierType(),
persister.getRootEntityName()
persister.getEntityName()
);

final Object ce = CacheHelper.fromSharedCache( source, ck, persister.getCacheAccessStrategy() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,36 @@
*/
package org.hibernate.test.cache.polymorphism;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;

/**
* @author Guillaume Smet
* @author Brett Meyer
*/
@TestForIssue(jiraKey = "HHH-9028")
public class PolymorphicCacheTest extends BaseCoreFunctionalTestCase {
@Override
protected void configure(Configuration configuration) {
super.configure( configuration );
}

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { AbstractCachedItem.class, CachedItem1.class, CachedItem2.class };
}

@Test
public void testPolymorphismAndCache() throws Exception {
final CachedItem1 cachedItem1 = new CachedItem1( "name 1" );
final CachedItem2 cachedItem2 = new CachedItem2( "name 2" );
final CachedItem1 item1 = new CachedItem1( "name 1" );
final CachedItem2 item2 = new CachedItem2( "name 2" );

// create the 2 items
Session s = openSession();
s.beginTransaction();
s.save( cachedItem1 );
s.save( cachedItem2 );
s.save( item1 );
s.save( item2 );
s.getTransaction().commit();
s.close();

Expand All @@ -64,16 +61,45 @@ public void testPolymorphismAndCache() throws Exception {
// As the first item is supposed to be a CachedItem1, it shouldn't be returned.
// Note that the Session API is not type safe but, when using the EntityManager.find API, you get a ClassCastException
// if calling find returns the object.
Object thisObjectShouldBeNull = s.get( CachedItem2.class, cachedItem1.getId() );
Object thisObjectShouldBeNull = s.get( CachedItem2.class, item1.getId() );
assertNull( thisObjectShouldBeNull );
s.getTransaction().commit();
s.close();

// test updating
s = openSession();
s.beginTransaction();
item1.setName( "updated" );
s.update( item1 );
s.getTransaction().commit();
s.clear();
s.beginTransaction();
CachedItem1 cachedItem1 = (CachedItem1) s.get( CachedItem1.class, item1.getId() );
CachedItem2 cachedItem2 = (CachedItem2) s.get( CachedItem2.class, item2.getId() );
assertEquals( "updated", cachedItem1.getName() );
assertEquals( item2.getName(), cachedItem2.getName() );
s.getTransaction().commit();
s.close();

// test deleting
s = openSession();
s.beginTransaction();
s.delete( item1 );
s.getTransaction().commit();
s.clear();
s.beginTransaction();
cachedItem1 = (CachedItem1) s.get( CachedItem1.class, item1.getId() );
cachedItem2 = (CachedItem2) s.get( CachedItem2.class, item2.getId() );
assertNull( cachedItem1 );
assertNotNull( cachedItem2 );
assertEquals( item2.getName(), cachedItem2.getName() );
s.getTransaction().commit();
s.close();

// cleanup
s = openSession();
s.beginTransaction();
s.delete( cachedItem1 );
s.delete( cachedItem2 );
s.createQuery( "DELETE FROM AbstractCachedItem" ).executeUpdate();
s.getTransaction().commit();
s.close();
}
Expand Down

0 comments on commit 768d02d

Please sign in to comment.