1313import org .hibernate .boot .Metadata ;
1414import org .hibernate .boot .MetadataSources ;
1515import org .hibernate .boot .SessionFactoryBuilder ;
16- import org .hibernate .cache .internal .CacheKeyImplementation ;
1716import org .hibernate .cache .internal .DefaultCacheKeysFactory ;
1817import org .hibernate .engine .spi .SessionFactoryImplementor ;
1918import org .hibernate .persister .entity .EntityPersister ;
2423import org .junit .Test ;
2524
2625import static org .junit .Assert .assertFalse ;
26+ import static org .junit .Assert .assertTrue ;
2727
2828/**
2929 * @author Gail Badner
@@ -32,37 +32,74 @@ public class CacheKeyImplementationHashCodeTest {
3232
3333 @ Test
3434 @ JiraKey ( value = "HHH-12746" )
35- public void test () {
35+ public void testHashCodeChanges () {
3636 try (ServiceRegistryImplementor serviceRegistry = ServiceRegistryUtil .serviceRegistry ()) {
3737 MetadataSources ms = new MetadataSources ( serviceRegistry );
3838 ms .addAnnotatedClass ( AnEntity .class ).addAnnotatedClass ( AnotherEntity .class );
3939 Metadata metadata = ms .buildMetadata ();
4040 final SessionFactoryBuilder sfb = metadata .getSessionFactoryBuilder ();
4141 try ( SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor ) sfb .build ()) {
42- CacheKeyImplementation anEntityCacheKey = createCacheKeyImplementation (
42+ Object anEntityCacheKey = createCacheKeyImplementation (
4343 1 ,
4444 sessionFactory .getRuntimeMetamodels ()
4545 .getMappingMetamodel ()
4646 .getEntityDescriptor ( AnEntity .class ),
47- sessionFactory
47+ sessionFactory ,
48+ "tenant"
4849 );
49- CacheKeyImplementation anotherEntityCacheKey = createCacheKeyImplementation (
50+ Object anotherEntityCacheKey = createCacheKeyImplementation (
5051 1 ,
5152 sessionFactory .getRuntimeMetamodels ()
5253 .getMappingMetamodel ()
5354 .getEntityDescriptor ( AnotherEntity .class ),
54- sessionFactory
55+ sessionFactory ,
56+ "tenant"
5557 );
5658 assertFalse ( anEntityCacheKey .equals ( anotherEntityCacheKey ) );
5759 }
5860 }
5961 }
6062
61- private CacheKeyImplementation createCacheKeyImplementation (
63+ @ Test
64+ @ JiraKey ( value = "HHH-19218" )
65+ public void testMixedHashCode () {
66+ try (ServiceRegistryImplementor serviceRegistry = ServiceRegistryUtil .serviceRegistry ()) {
67+ MetadataSources ms = new MetadataSources ( serviceRegistry );
68+ ms .addAnnotatedClass ( AnEntity .class ).addAnnotatedClass ( AnotherEntity .class );
69+ Metadata metadata = ms .buildMetadata ();
70+ final SessionFactoryBuilder sfb = metadata .getSessionFactoryBuilder ();
71+ try ( SessionFactoryImplementor sessionFactory = (SessionFactoryImplementor ) sfb .build ()) {
72+ Object anEntityCacheKey = createCacheKeyImplementation (
73+ 1 ,
74+ sessionFactory .getRuntimeMetamodels ()
75+ .getMappingMetamodel ()
76+ .getEntityDescriptor ( AnEntity .class ),
77+ sessionFactory ,
78+ null
79+ );
80+ assertTrue ( (anEntityCacheKey .hashCode () >>> 16 ) != 0 );
81+ assertTrue ( (anEntityCacheKey .hashCode () << 16 ) != 0 );
82+
83+ Object anotherEntityCacheKey = createCacheKeyImplementation (
84+ 1 ,
85+ sessionFactory .getRuntimeMetamodels ()
86+ .getMappingMetamodel ()
87+ .getEntityDescriptor ( AnotherEntity .class ),
88+ sessionFactory ,
89+ "0"
90+ );
91+ assertTrue ( (anotherEntityCacheKey .hashCode () >>> 16 ) != 0 );
92+ assertTrue ( (anotherEntityCacheKey .hashCode () << 16 ) != 0 );
93+ }
94+ }
95+ }
96+
97+ private Object createCacheKeyImplementation (
6298 int id ,
6399 EntityPersister persister ,
64- SessionFactoryImplementor sfi ) {
65- return (CacheKeyImplementation ) DefaultCacheKeysFactory .staticCreateEntityKey ( id , persister , sfi , "tenant" );
100+ SessionFactoryImplementor sfi ,
101+ String tenantIdentifier ) {
102+ return DefaultCacheKeysFactory .staticCreateEntityKey ( id , persister , sfi , tenantIdentifier );
66103 }
67104
68105 @ Entity (name = "AnEntity" )
0 commit comments