0
        2016-01-28 12:27:46.433 3a37210f D 00000000000000000000000000000000    002e:Could not retrieve pre-bound Hibernate session  "org.hibernate.HibernateException: No Session found for current thread
            at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:106)
            at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
            at org.springframework.orm.hibernate4.HibernateTemplate.doExecute(HibernateTemplate.java:325)
            at org.springframework.orm.hibernate4.HibernateTemplate.execute(HibernateTemplate.java:295)
            at com.fusionone.pml.dao.hibernate.AbstractDao.executeCallback(AbstractDao.java:398)
            at com.fusionone.pml.dao.hibernate.AbstractDao.listInner(AbstractDao.java:300)
            at com.fusionone.pml.dao.hibernate.AbstractDao.list(AbstractDao.java:262)
            at com.fusionone.pml.dao.hibernate.ExtShareDaoImpl.listExpiredShares(ExtShareDaoImpl.java:63)
            at com.fusionone.nab.apps.service.impl.ExtShareServiceImpl.listExpiredShares(ExtShareServiceImpl.java:628)
            at com.fusionone.wsg.nab.scheduler.ExtShareExpiredScheduler.cleanup(ExtShareExpiredScheduler.java:111)
            at com.fusionone.wsg.nab.scheduler.ExtShareExpiredScheduler.execute(ExtShareExpiredScheduler.java:99)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:497)
            at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:269)
            at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:321)
            at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:111)
            at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
            at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)
        "
        2016-01-28 12:27:46.441 3a37210f D 00000000000000000000000000000000    000b:F1Exception  "8 SQL Error  (Named query not known: ExtShare.find_all_shares_expired; nested exception is org.hibernate.MappingException: Named query not known: ExtShare.find_all_shares_expired)
            at com.fusionone.pml.dao.hibernate.AbstractDao.executeCallback(AbstractDao.java:400)
            at com.fusionone.pml.dao.hibernate.AbstractDao.listInner(AbstractDao.java:300)
            at com.fusionone.pml.dao.hibernate.AbstractDao.list(AbstractDao.java:262)
            at com.fusionone.pml.dao.hibernate.ExtShareDaoImpl.listExpiredShares(ExtShareDaoImpl.java:63)
            at com.fusionone.nab.apps.service.impl.ExtShareServiceImpl.listExpiredShares(ExtShareServiceImpl.java:628)
            at com.fusionone.wsg.nab.scheduler.ExtShareExpiredScheduler.cleanup(ExtShareExpiredScheduler.java:111)
            at com.fusionone.wsg.nab.scheduler.

我升级到 hibernate4.3.11 我也在我的项目中使用 spring.4.0。

我正在通过扩展 localsessionfactorybean 方法 buildsessionfactorymethod 来加载会话工厂和映射资源。

    @Override
        protected  SessionFactory buildSessionFactory(LocalSessionFactoryBuilder localSessionFactoryBuilder){
            SessionFactory sessionFactory = null;
            PMLDialectMappings dialectMappings = null;
             String[] mappingResources = null;
            try {

                Configuration configuration = new Configuration();
                            // building mappingLocations

    Configuration configuration = new Configuration();
                            // building mappingLocations
                 dialectMappings =
                        dialectMappingsLocator.locateDialectMappings(configuration);
                if (dialectMappings != null) {
                    setMappingResources(dialectMappings.getAllMappingResources());
                    AbstractDao.setDialectSettings(dialectMappings);
                }

        ServiceRegistry  builder =  new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
                sessionFactory = configuration.buildSessionFactory(builder);

    String schema = configuration.getProperty("hibernate.default_schema");


            return super.buildSessionFactory(localSessionFactoryBuilder);
}

上面的代码创建会话工厂是否正确?我看到了上面创建会话工厂的方法,并且我正在以相同的方法加载映射资源。这是正确的还是不正确的?之前在 hibernate3 中,我们也在 getconfiguration 方法中加载映射资源。这在 Hibernate4 中已被弃用,所以我想只在 buildSessionFactory 方法中加载映射资源。

同样在谷歌中,如果您在属性下方添加“无休眠会话”错误将会消失,但它对我不起作用。所以请提出其他建议。

hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext

hibernate 3中的代码是构建sessionfactory和加载mappingresource

@Override
    protected SessionFactory newSessionFactory(Configuration config) {
        try {
            String schema = config.getProperty("hibernate.default_schema");
            if (schema != null && schema.length() > 0) {
                AbstractDao.setSchemaName(schema);
            } else {
                Logger.get(Constants.PML_LOGGER).warning(
                    "Property 'hibernate.default_schema' not set: using default");
            }
        } catch (Throwable t) {
            Logger.get(Constants.PML_LOGGER).exception(LogType.CRITICAL, null, null,
                "PMLSessionFactoryBean: newSessionFactory method failed", t);
            throw new HibernateException(t.getMessage());
        }
        return super.newSessionFactory(config);
    }

    /**
     * @return Configuration
     * @see org.springframework.orm.hibernate3.LocalSessionFactoryBean#newConfiguration()
     */
    @Override
    protected Configuration newConfiguration() {
        Configuration configuration = super.newConfiguration();
        // building mappingLocations
        PMLDialectMappings dialectMappings =
            dialectMappingsLocator.locateDialectMappings(configuration);
        if (dialectMappings != null) {
            setMappingResources(dialectMappings.getAllMappingResources());
            AbstractDao.setDialectSettings(dialectMappings);
        }
        return configuration;
    }

}


application-context.xml 
*********************
 <bean id="PMLJdbcProperties" class="com.fusionone.core.util.FilteredProperties">
        <property name="location" value="classpath:jdbc.properties" />
        <constructor-arg value="pml" />
    </bean>
    <bean id="PMLDataSource"
        class="com.fusionone.core.db.pool.MonitoredDataSourceFactory"
        destroy-method="close" factory-method="createDataSource">
        <constructor-arg ref="PMLJdbcProperties" />
    </bean>
    <bean id="PMLDefaultDialectSettings" class="com.fusionone.pml.bean.PMLDialectMappings"
        lazy-init="true" autowire-candidate="false">
        <property name="mappingResources">
            <list>
                <value>hibernate-mappings/BrewApp.hbm.xml</value>
                <value>hibernate-mappings/Contact.hbm.xml</value>
                <value>hibernate-mappings/DeletedContact.hbm.xml</value>
                <value>hibernate-mappings/DeletedContactGroup.hbm.xml</value>
</list>
</property>

        <property name="daoInterfaceImpls">
            <map>
                <entry key="DeletedContactDao"
                    value="com.fusionone.pml.dao.hibernate.DeletedContactDaoImpl" />
                <entry key="DeletedContactGroupDao"
                    value="com.fusionone.pml.dao.hibernate.DeletedContactGroupDaoImpl" />
</map>
</property>

    <bean id="dialectMappingsLocator" class="com.fusionone.pml.bean.DialectMappingsLocator"
        autowire="byType">
    </bean>
    <bean id="PMLSessionFactory" class="com.fusionone.pml.bean.PMLSessionFactoryBean">
        <property name="dataSource">
            <ref bean="PMLDataSource" />
        </property>
        <property name="dialectMappingsLocator" ref="dialectMappingsLocator" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.jdbc.use_get_generated_keys">
                    false
                </prop>
                <prop key="hibernate.cache.provider_class">
                    net.sf.ehcache.hibernate.EhCacheProvider
                </prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.jdbc.batch_size">150</prop>
            </props>
        </property>
    </bean> 

<tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="PMLSessionFactory" />
        </property>
        <property name="nestedTransactionAllowed" value="true" />
    </bean>

Hibernate 4 更改了缓存 regionfactory 并将事务管理器更改为 hibernate 4。

我将应用程序上下文 xml 更改为

 <bean id="PMLJdbcProperties" class="com.fusionone.core.util.FilteredProperties">
        <property name="location" value="classpath:jdbc.properties" />
        <constructor-arg value="pml" />
    </bean>
    <bean id="PMLDataSource"
        class="com.fusionone.core.db.pool.MonitoredDataSourceFactory"
        destroy-method="close" factory-method="createDataSource">
        <constructor-arg ref="PMLJdbcProperties" />
    </bean>
    <bean id="PMLDefaultDialectSettings" class="com.fusionone.pml.bean.PMLDialectMappings"
        lazy-init="true" autowire-candidate="false">
        <property name="mappingResources">
            <list>
                <value>hibernate-mappings/BrewApp.hbm.xml</value>
                <value>hibernate-mappings/Contact.hbm.xml</value>
                <value>hibernate-mappings/DeletedContact.hbm.xml</value>
                <value>hibernate-mappings/DeletedContactGroup.hbm.xml</value>
</list>
</property>

        <property name="daoInterfaceImpls">
            <map>
                <entry key="DeletedContactDao"
                    value="com.fusionone.pml.dao.hibernate.DeletedContactDaoImpl" />
                <entry key="DeletedContactGroupDao"
                    value="com.fusionone.pml.dao.hibernate.DeletedContactGroupDaoImpl" />
</map>
</property>

    <bean id="dialectMappingsLocator" class="com.fusionone.pml.bean.DialectMappingsLocator"
        autowire="byType">
    </bean>
    <bean id="PMLSessionFactory" class="com.fusionone.pml.bean.PMLSessionFactoryBean">
        <property name="dataSource">
            <ref bean="PMLDataSource" />
        </property>
        <property name="dialectMappingsLocator" ref="dialectMappingsLocator" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.jdbc.use_get_generated_keys">
                    false
                </prop>
                <prop key="hibernate.cache.region.provider_class">
                org.hibernate.cache.ehcache.EhCacheRegionFactory
            </prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.jdbc.batch_size">150</prop>
            </props>
        </property>
    </bean> 

<tx:annotation-driven transaction-manager="transactionManager" />
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="PMLSessionFactory" />
        </property>
        <property name="nestedTransactionAllowed" value="true" />
    </bean>

主要是我需要知道如何创建sessionfactory和如何加载mappingresources。

4

1 回答 1

0

更改了我的localsessionfactorybean代码,如下所示。然后正确加载所有映射资源。hibernate 3 配置中的先前配置是在加载之前加载所有映射sessionfactorybean。但是在hiberate4中不是这样的,你必须手动将资源添加到localsessionfactorybean中。我添加的同样的东西它的工作。我认为setmappingresources负载足够了,但你也需要addresourcebean localsessionfactory

dialectMappings = dialectMappingsLocator.locateDialectMappings(configuration);

if (dialectMappings != null) {
    setMappingResources(dialectMappings.getAllMappingResources());
    AbstractDao.setDialectSettings(dialectMappings);
}

for(String resource : dialectMappings.getParentDialectMappings().getAllMappingResources()) {
    configuration.addResource(resource);
}

之后我实施了适当的实施nullsafegetnullsafeset现在我们的项目已完全升级到休眠 4.3.11。最后,我们能够为我的所有查询添加查询提示,这有助于强制对表进行索引。感谢 stackoverflow 的帮助。

于 2016-02-07T07:00:44.897 回答