Hibernate 4多租户问题 - 找不到实体持久性

时间:2015-06-25 11:18:01

标签: java spring hibernate jpa

我一直在尝试在我们的应用程序中集成hibernate 4多租户支持,但是在执行hql查询时遇到以下异常

java.lang.ArrayIndexOutOfBoundsException: 0
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.resultClassChecking(AbstractEntityManagerImpl.java:362)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:344)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:344)
    at com.sun.proxy.$Proxy288.createQuery(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:291)
    at com.sun.proxy.$Proxy43.createQuery(Unknown Source)

hql查询是 -

List<Field> fields = entityManager.createQuery("from " + Employee.class.getName() +
                    " where " + getQueryForInClause("id", ids), Employee.class).getResultList();

在调试hibernate源代码时,我意识到这是因为hibernate的SessionFactory实例没有任何entityPersister实例,因为hql查询确实有任何translator。

是因为Entity bean没有被扫描?如果是这样可能是什么原因?

entityFactory spring声明如下

         

<property name="persistenceProviderClass" value="org.hibernate.jpa.HibernatePersistenceProvider"/>
<property name="persistenceXmlLocation" value="/com/software/persistence/persistence.xml"/>
<property name="jpaProperties">
    <map>
        <entry key="hibernate.id.new_generator_mappings" value="true"/>
        <entry key="hibernate.cache.use_second_level_cache" value="false"/>
        <entry key="hibernate.dialect" value="com.software.persistence.ExtendedOracle10gDialect"/>
        <entry key="hibernate.jdbc.batch_size" value="10"/>
        <entry key="hibernate.jdbc.batch_versioned_data" value="true"/>
        <entry key="hibernate.jdbc.batch.builder" value="com.software.persistence.OracleBatchBuilder"/>
        <entry key="hibernate.multiTenancy" value="DATABASE"/>
        <entry key="hibernate.tenant_identifier_resolver"
                  value="com.software.persistence.MultitenantIdentifierResolver"/>
        <entry key="hibernate.multi_tenant_connection_provider"
                  value-ref="multiTenantConnectionProvider" />
    </map>
</property>

1 个答案:

答案 0 :(得分:1)

大多数错误似乎是因为实体bean没有被扫描。

您是否也可以共享persistence.xml以供参考。

以下可以尝试进一步调试(其中一步可能解决我猜的问题)

1使用classpath指定persistenceXmlLocation作为此<property name="persistenceXmlLocation" value="classpath:/com/software/persistence/persistence.xml"/>

2尝试使用persistenceUnitPostProcessors来扫描像这样的bean <property name="persistenceUnitPostProcessors" ref="some_implementation_bean_ref"/>