HHH-11532 - load by natural-id and WrongClassException

This commit is contained in:
Steve Ebersole 2017-12-16 20:24:54 -06:00
parent 9529f4f0a2
commit b7c0235697

View File

@ -6,22 +6,16 @@
*/
package org.hibernate.test.naturalid.inheritance.cache;
import org.hibernate.WrongClassException;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
@FailureExpected( jiraKey = "HHH-11532" )
public class InheritedNaturalIdCacheTest extends BaseCoreFunctionalTestCase {
@Override
@ -41,110 +35,71 @@ protected boolean isCleanupTestDataRequired() {
}
@Test
public void testLoadExtendedByNormal() {
doInHibernate( this::sessionFactory, session -> {
session.save( new MyEntity( "base" ) );
session.save( new ExtendedEntity( "extended", "ext" ) );
});
public void testLoadingInheritedEntitiesByNaturalId() {
// create the data:
// MyEntity#1
// ExtendedEntity#1
doInHibernate( this::sessionFactory, session -> {
// Sanity check, ensure both entities is accessible.
MyEntity user = session.byNaturalId( MyEntity.class ).using(
"uid",
"base"
).load();
ExtendedEntity extendedMyEntity = session.byNaturalId(
ExtendedEntity.class )
.using( "uid", "extended" )
.load();
assertNotNull( user );
assertNotNull( extendedMyEntity );
} );
inTransaction(
session -> {
session.save( new MyEntity( "base" ) );
session.save( new ExtendedEntity( "extended", "ext" ) );
}
);
doInHibernate( this::sessionFactory, session -> {
// This throws WrongClassException, since MyEntity was found using the ID, but we wanted ExtendedEntity.
ExtendedEntity user = session.byNaturalId( ExtendedEntity.class )
.using( "uid", "base" )
.load();
assertNull( user );
} );
}
// load the entities "properly" by natural-id
@Test
public void testLoadExtendedByNormalCatchingWrongClassException() {
doInHibernate( this::sessionFactory, session -> {
session.save( new MyEntity( "normal" ) );
session.save( new ExtendedEntity( "extended", "ext" ) );
});
inTransaction(
session -> {
final MyEntity entity = session.bySimpleNaturalId( MyEntity.class ).load( "base" );
assertNotNull( entity );
doInHibernate( this::sessionFactory, session -> {
MyEntity user = session.byNaturalId( MyEntity.class ).using(
"uid",
"normal"
).load();
ExtendedEntity extendedMyEntity = session.byNaturalId(
ExtendedEntity.class )
.using( "uid", "extended" )
.load();
assertNotNull( user );
assertNotNull( extendedMyEntity );
} );
final ExtendedEntity extendedEntity = session.bySimpleNaturalId( ExtendedEntity.class ).load( "extended" );
assertNotNull( extendedEntity );
}
);
doInHibernate( this::sessionFactory, session -> {
session.byNaturalId( ExtendedEntity.class ).using(
"uid",
"normal"
).load();
} );
// finally, attempt to load MyEntity#1 as an ExtendedEntity, which should
// throw a WrongClassException
inTransaction(
session -> {
try {
session.bySimpleNaturalId( ExtendedEntity.class ).load( "base" );
fail( "Expecting WrongClassException" );
}
catch (WrongClassException expected) {
// expected outcome
}
catch (Exception other) {
throw new AssertionError(
"Unexpected exception type : " + other.getClass().getName(),
other
);
}
}
);
// this is functionally equivalent to loading the wrong class by id...
inTransaction(
session -> {
try {
session.byId( ExtendedEntity.class ).load( 1L );
fail( "Expecting WrongClassException" );
}
catch (WrongClassException expected) {
// expected outcome
}
catch (Exception other) {
throw new AssertionError(
"Unexpected exception type : " + other.getClass().getName(),
other
);
}
}
);
}
@Test
public void testLoadExtendedByNormalCatchingWrongClassException2() {
doInHibernate( this::sessionFactory, session -> {
session.save( new MyEntity( "normal" ) );
session.save( new ExtendedEntity( "extended", "ext" ) );
});
doInHibernate( this::sessionFactory, session -> {
MyEntity user = session.byNaturalId( MyEntity.class ).using(
"uid",
"normal"
).load();
ExtendedEntity extendedMyEntity = session.byNaturalId(
ExtendedEntity.class )
.using( "uid", "extended" )
.load();
assertNotNull( user );
assertNotNull( extendedMyEntity );
} );
// Temporarily change logging level for these two classes to DEBUG
final Logger afelLogger = LogManager.getLogger(
"org.hibernate.event.internal.AbstractFlushingEventListener" );
final Logger epLogger = LogManager.getLogger(
"org.hibernate.internal.util.EntityPrinter" );
final Level afelLevel = afelLogger.getLevel();
final Level epLevel = epLogger.getLevel();
try {
// this throws if logging level is set to debug
doInHibernate( this::sessionFactory, session -> {
afelLogger.setLevel( Level.DEBUG );
epLogger.setLevel( Level.DEBUG );
session.byNaturalId( ExtendedEntity.class ).using(
"uid",
"normal"
).load();
} );
}
finally {
// set back previous logging level
afelLogger.setLevel( afelLevel );
epLogger.setLevel( epLevel );
}
}
}