I'm trying to use multi-tenancy support with hibernate core 4.0.1. Here an extract from my code to create the session factory:
Code:
Configuration conf = new Configuration();
conf.addResource(getCS5PatchLevelMappingFile(_poolVersion.getPatchLevel()));
conf.setProperty(Environment.DIALECT, MySQL5Dialect.class.getName());
conf.getProperties().put(Environment.MULTI_TENANT, MultiTenancyStrategy.DATABASE);
ServiceRegistryBuilder registryBuilder = new ServiceRegistryBuilder();
registryBuilder.applySettings(conf.getProperties());
registryBuilder.addService( MultiTenantConnectionProvider.class, getPool() );
ServiceRegistryImplementor serviceRegistry = (ServiceRegistryImplementor) registryBuilder.buildServiceRegistry();
sessionFactory = conf.buildSessionFactory(serviceRegistry);
However one of my entities uses the "org.hibernate.id.enhanced.TableGenerator" for an ID generator, which should return the ID generated by the table itself. This works ok without using multi-tenancy but runs into a NullPointerException when saving the entity using multi-tenancy:
Code:
java.lang.NullPointerException
at org.hibernate.engine.transaction.internal.jdbc.JdbcIsolationDelegate.delegateWork(JdbcIsolationDelegate.java:68)
at org.hibernate.id.enhanced.TableGenerator$1.getNextValue(TableGenerator.java:471)
at org.hibernate.id.enhanced.OptimizerFactory$NoopOptimizer.generate(OptimizerFactory.java:252)
at org.hibernate.id.enhanced.TableGenerator.generate(TableGenerator.java:467)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:120)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:204)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:189)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:642)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:635)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:631)
I've looked into the code. The JdbcIsolationDelegate tries to retrieve a ConnectionProvider from org.hibernate.engine.jdbc.internal.JdbcServicesImpl. While JdbcServicesImpl seems to have support for multi-tenancy internally it does not return a connection provider in that case via getConnectionProvider().
Am I doing something wrong here or is this TableGenerator currently incompatible with multi-tenancy? Where would it need to retrieve its connection from?
Thanks for any help!