use of org.hibernate.test.multitenancy.schema.Customer in project hibernate-orm by hibernate.
the class DiscriminatorMultiTenancyTest method testDiscriminator.
/**
* This tests for current tenant being used for second level cache, but not selecting connection provider.
* Discrimination on connection level will for now need to be implemented in the supplied connection provider.
*/
@Test
public void testDiscriminator() {
doInHibernate("jboss", session -> {
Customer steve = new Customer(1L, "steve");
session.save(steve);
});
sessionFactory.getStatistics().clear();
// make sure we get the steve back, from cache if same tenant (jboss)
doInHibernate("jboss", session -> {
Customer customer = session.load(Customer.class, 1L);
Assert.assertEquals("steve", customer.getName());
// also, make sure this came from second level
Assert.assertEquals(1, sessionFactory.getStatistics().getSecondLevelCacheHitCount());
});
sessionFactory.getStatistics().clear();
// then make sure we get the steve back, from db if other tenant (acme)
doInHibernate("acme", session -> {
Customer customer = session.load(Customer.class, 1L);
Assert.assertEquals("steve", customer.getName());
// also, make sure this doesn't came from second level
Assert.assertEquals(0, sessionFactory.getStatistics().getSecondLevelCacheHitCount());
});
// make sure the same works from data store too
sessionFactory.getStatistics().clear();
sessionFactory.getCache().evictEntityRegions();
// first jboss
doInHibernate("jboss", session -> {
Customer customer = session.load(Customer.class, 1L);
Assert.assertEquals("steve", customer.getName());
// also, make sure this doesn't came from second level
Assert.assertEquals(0, sessionFactory.getStatistics().getSecondLevelCacheHitCount());
});
sessionFactory.getStatistics().clear();
// then, acme
doInHibernate("acme", session -> {
Customer customer = session.load(Customer.class, 1L);
Assert.assertEquals("steve", customer.getName());
// also, make sure this doesn't came from second level
Assert.assertEquals(0, sessionFactory.getStatistics().getSecondLevelCacheHitCount());
});
doInHibernate("jboss", session -> {
Customer customer = session.load(Customer.class, 1L);
session.delete(customer);
});
}