yujian created Bug HHH-7389
Issue Type: Bug Bug
Affects Versions: 4.1.3
Assignee: Unassigned
Components: core
Created: 13/Jun/12 8:58 PM
Description:

Im using schema based multi-tenancy and entity id generation strategy is TABLE.When I save a new entity into database, NullPointerException occur.

java.lang.NullPointerException
at org.hibernate.engine.transaction.internal.jdbc.JdbcIsolationDelegate.delegateWork(JdbcIsolationDelegate.java:68)
at org.hibernate.id.TableGenerator.generateHolder(TableGenerator.java:148)
at org.hibernate.id.TableHiLoGenerator$1.getNextValue(TableHiLoGenerator.java:84)
at org.hibernate.id.enhanced.OptimizerFactory$LegacyHiLoAlgorithmOptimizer.generate(OptimizerFactory.java:393)
at org.hibernate.id.TableHiLoGenerator.generate(TableHiLoGenerator.java:81)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:118)
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:757)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:749)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:745)

Then I dig into hibernate source code, and find that:
When hibernate's MultiTenancyStrategy is not NONE, JdbcServicesImpl.connectionProvider will must be null.And when id generation strategy is table, hibernate will use hilo algorithm to generate id, and use JdbcIsolationDelegate.delegateWork do this work.But dJdbcIsolationDelegate.delegateWork get connection from JdbcServicesImpl.connectionProvider, then NullPointerException occur.

hibernate's source code:

public class JdbcServicesImpl implements ... {

...
private JdbcConnectionAccess buildJdbcConnectionAccess(Map configValues) {
final MultiTenancyStrategy multiTenancyStrategy = MultiTenancyStrategy.determineMultiTenancyStrategy( configValues );

if ( MultiTenancyStrategy.NONE == multiTenancyStrategy ) { connectionProvider = serviceRegistry.getService( ConnectionProvider.class ); return new ConnectionProviderJdbcConnectionAccess( connectionProvider ); }
else { connectionProvider = null; final MultiTenantConnectionProvider multiTenantConnectionProvider = serviceRegistry.getService( MultiTenantConnectionProvider.class ); return new MultiTenantConnectionProviderJdbcConnectionAccess( multiTenantConnectionProvider ); }
}
...
}

public class JdbcIsolationDelegate implements IsolationDelegate {
public <T> T delegateWork(WorkExecutorVisitable<T> work, boolean transacted) throws HibernateException {
boolean wasAutoCommit = false;
try { // todo : should we use a connection proxy here? Connection connection = connectionProvider().getConnection(); .... }

Environment: Hibernate 4.1.3, using JPA annotations
Project: Hibernate ORM
Priority: Major Major
Reporter: yujian
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators.
For more information on JIRA, see: http://www.atlassian.com/software/jira