Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaSpring:AnnotationSessionFactoryBean,Hibernate方言自动检测_Spring_Hibernate_Dialect - Fatal编程技术网

JavaSpring:AnnotationSessionFactoryBean,Hibernate方言自动检测

JavaSpring:AnnotationSessionFactoryBean,Hibernate方言自动检测,spring,hibernate,dialect,Spring,Hibernate,Dialect,我使用以下spring应用程序上下文: <!-- Hibernate session factory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property

我使用以下spring应用程序上下文:

<!-- Hibernate session factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="hibernateProperties">
      <props>
        <!--
             <prop key="hibernate.dialect">${hibernate.dialect}</prop>
          -->
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.max_fetch_depth">${hibernate.max_fetch_depth}</prop>

            <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
            <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
            <prop key="hibernate.cache.provider_class">${hibernate.cache.provider}</prop>

            <prop key="hibernate.connection.pool_size">10</prop>
            <prop key="hibernate.jdbc.batch_size">1000</prop>
            <prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>

        </props>
    </property>
    <property name="annotatedClasses">
        <list>
          ...
       </list>
    </property>

    <property name="schemaUpdate" value="${hibernate.schemaUpdate}"/>
</bean>
我可以通过取消hibernate.dial属性的注释来避免此异常。我为什么要这么做?为什么hibernate不能像预期的那样自动检测方言


我想删除方言属性以解决问题。

Hibernate本身尝试在
配置期间自动检测方言。buildSessioNFactory()
调用通常会成功,但并不总是成功

然而,Spring代码却不是这样——这就是您得到异常的地方
LocalSessionFactoryBean.updateDatabaseSchema()
方法使用
Dialogue.GetDialogue()
方法获取需要显式设置Dialogue的
Dialogue
实例

我不太清楚为什么会这样做,尽管我怀疑这是为了防止Hibernate错误地检测到您的方言,并因此弄乱您的数据库(因为更新模式可能会带来灾难性后果)

你的选择是:

  • 显式指定方言
  • schemaUpdate
    设置为false
  • 扩展LocalSessionFactoryBean并重写
    updateDatabaseSchema()
    方法以从会话工厂实例获取(已自动检测到的)方言
  • 为此,您必须将其类别转换为SessionFactoryImplementor:

    public void updateDatabaseSchema() throws DataAccessException {
      final Dialect dialect = ((SessionFactoryImplementor) getSessionFactory()).getDialect();
      ...
      hibernateTemplate.execute(new HibernateCallback() {
        ...
        // you already have the dialect so you don't need to get it using this:
        // Dialect dialect = Dialect.getDialect(getConfiguration().getProperties());
        ...
      );
    }
    
    public void updateDatabaseSchema() throws DataAccessException {
      final Dialect dialect = ((SessionFactoryImplementor) getSessionFactory()).getDialect();
      ...
      hibernateTemplate.execute(new HibernateCallback() {
        ...
        // you already have the dialect so you don't need to get it using this:
        // Dialect dialect = Dialect.getDialect(getConfiguration().getProperties());
        ...
      );
    }