JavaSpring:AnnotationSessionFactoryBean,Hibernate方言自动检测
我使用以下spring应用程序上下文: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
<!-- 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
设置为falseupdateDatabaseSchema()
方法以从会话工厂实例获取(已自动检测到的)方言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());
...
);
}