org.hibernate HibernateException Dialect must be explicitly set :*** 
使用Hibernate,有时候会遇到类似上面的异常。 
使用代码获取Session是要使用Configuration要调用Configure方法。这个方法很容易被遗忘。
 
SessionFactory sf=config.configure().buildSessionFactory(); Session session=sf.openSession();

通过API可以知道configure()方法的作用如下:

Use the mappings and properties specified in an application resource named hibernate.cfg.xml
configure()方法默认加载hibernate.cfg.xml文件,使该文件的映射和配置在应用程序中使用;configure()还有重载的方法来指定配置分别是
 configure(Document document)
configure(File configFile)
configure(String resource)
configure(URL url)

  

若使用configure方法那么就可以在hibernate.cfg.xml中配置数据库的dialect:
      

<property name="dialect">数据库对应的dialect</property>
通过配置来指定数据库对于的的dialect,具体的数据库和数据库对于的dialect如下:
 
 
RDBMS Dialect
DB2 org.hibernate.dialect.DB2Dialect
DB2 AS/400 org.hibernate.dialect.DB2400Dialect
DB2 OS390 org.hibernate.dialect.DB2390Dialect
PostgreSQL org.hibernate.dialect.PostgreSQLDialect
MySQL org.hibernate.dialect.MySQLDialect
MySQL with InnoDB org.hibernate.dialect.MySQLInnoDBDialect
MySQL with MyISAM org.hibernate.dialect.MySQLMyISAMDialect
Oracle (any version) org.hibernate.dialect.OracleDialect
Oracle 9i/10g org.hibernate.dialect.Oracle9Dialect
Sybase org.hibernate.dialect.SybaseDialect
Sybase Anywhere org.hibernate.dialect.SybaseAnywhereDialect
Microsoft SQL Server org.hibernate.dialect.SQLServerDialect
SAP DB org.hibernate.dialect.SAPDBDialect
Informix org.hibernate.dialect.InformixDialect
HypersonicSQL org.hibernate.dialect.HSQLDialect
Ingres org.hibernate.dialect.IngresDialect
Progress org.hibernate.dialect.ProgressDialect
Mckoi SQL org.hibernate.dialect.MckoiDialect
Interbase org.hibernate.dialect.InterbaseDialect
Pointbase org.hibernate.dialect.PointbaseDialect
FrontBase org.hibernate.dialect.FrontbaseDialect
Firebird org.hibernate.dialect.FirebirdDialect
 
 
使用spring和hibernate时如果使用时,有时候会去掉hibernate.cfg.xml,那么则要在Spring中配置dialect
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.format_sql">true</prop>
<!-- 配置hibernate.dialect-->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
</props>
</property>

  

 
 
通过Hibernate Reference 可以知道,Dialect这部分内容使和数据库的移植性有相关的,在3.2之前的版本,Hibernate是要求用户明确指定数据库的Dialect的,3.2或以上版本通过java.sql.DataBaseMetaData来确定数据库;不过该文档同时指出来这种方法局限于Hibernate已知的数据库且无法进行配置和覆盖。3.3以后,用户可以通过重写org.hibernate.resolver.DialectResolver的resolveDialect方法来自定义方言,“要注册一个或多个解析者,只要用 'hibernate.dialect_resolvers' 配置设置指定它们(由逗号、制表符或空格隔开)就可以了(请参考 org.hibernate.cfg.Environment 上的 DIALECT_RESOLVERS)”。
用户定义的类通过AvailableSettings.DIALECT_RESOLVERS 获取类名,然后通过反射来注册DialectResolver。AvailableSettings.DIALECT_RESOLVERS 配置的类在determineResolvers链上先于StandardDialectResolver,所以自定义的解释者更优先处理DatabaseMetaData。
以下使hibernate初始化DialectResolver链的类。
import org.hibernate.HibernateException;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.service.classloading.spi.ClassLoaderService;
import org.hibernate.service.spi.ServiceException;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import org.hibernate.service.jdbc.dialect.spi.DialectResolver;
import org.hibernate.service.spi.BasicServiceInitiator; /**
* Standard initiator for the standard {@link DialectResolver} service
*
* @author Steve Ebersole
*/
public class DialectResolverInitiator implements BasicServiceInitiator<DialectResolver> {
public static final DialectResolverInitiator INSTANCE = new DialectResolverInitiator(); @Override
public Class<DialectResolver> getServiceInitiated() {
return DialectResolver.class;
} @Override
public DialectResolver initiateService(Map configurationValues, ServiceRegistryImplementor registry) {
return new DialectResolverSet( determineResolvers( configurationValues, registry ) );
} private List<DialectResolver> determineResolvers(Map configurationValues, ServiceRegistryImplementor registry) {
final List<DialectResolver> resolvers = new ArrayList
(); final String resolverImplNames = (String) configurationValues.get( AvailableSettings.DIALECT_RESOLVERS ); if ( StringHelper.isNotEmpty( resolverImplNames ) ) {
final ClassLoaderService classLoaderService = registry.getService( ClassLoaderService.class );
for ( String resolverImplName : StringHelper.split( ", \n\r\f\t", resolverImplNames ) ) {
try {
resolvers.add( (DialectResolver) classLoaderService.classForName( resolverImplName ).newInstance() );
}
catch (HibernateException e) {
throw e;
}
catch (Exception e) {
throw new ServiceException( "Unable to instantiate named dialect resolver [" + resolverImplName + "]", e );
}
}
} resolvers.add( new StandardDialectResolver() );
return resolvers;
}
}
 

关于Hibernate的Dialect的更多相关文章

  1. Hibernate SQL Dialect 方言

    RDBMS Dialect DB2 org.hibernate.dialect.DB2Dialect DB2 AS/400 org.hibernate.dialect.DB2400Dialect DB ...

  2. Hibernate 的dialect 大全

    RDBMS 方言 DB2 org.hibernate.dialect.DB2Dialect DB2 AS/400 org.hibernate.dialect.DB2400Dialect DB2 OS3 ...

  3. Hibernate的dialect大全

    RDBMS 方言 DB2 org.hibernate.dialect.DB2Dialect DB2 AS/400 org.hibernate.dialect.DB2400Dialect DB2 OS3 ...

  4. hibernate中dialect的讲解

    RDBMS方言 DB2 org.hibernate.dialect.DB2Dialect DB2 AS/400 org.hibernate.dialect.DB2400Dialect DB2 OS39 ...

  5. 【Hibernate】Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

    今天用hibernate框架写crm项目时遇到报错: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' n ...

  6. Hibernate工作原理

    现在我们知道了一个概念Hibernate Session,只有处于Session管理下的POJO才具有持久化操作能力.当应用程序对于处于Session管理下的POJO实例执行操作时,Hibernate ...

  7. [转]Hibernate不能自动建表解决办法及Hibernate不同数据库的连接及SQL方言

    最近开始学Hibernate,看的是李刚的那本<轻量级java ee企业应用实战>.头一个hibernate程序,我原原本本的按照书上例子写下来,同时只是改动了些mysql的连接参数,并且 ...

  8. 让Hibernate生成的DDL脚本自动增加注释

    我们知道可以通过Hibernate对象自动生成DDL建表语句,通过PowerDesigner工具可以反向工程生成数据字典,但是在生成的DDL中一直不能写上中文的注释,这就使我们生成的数据字典不具有可用 ...

  9. hibernate 对 sql server 2005 分页改进

    Hibernate 可以实现分页查询 如下 Query q = session.createQuery("from Cat as c"); q.setFirstResult(100 ...

随机推荐

  1. eclipse自动排版JSP问题

    eclipse自动排版JSP非常难看,标签每行显示不完整,开发时很难受,下面设置一下这个就好多了: window-->preferences-->Web-->HTML Files-- ...

  2. hashMap和hashTable的区别

    每日总结,每天进步一点点 hashMap和hashTable的区别 1.父类:hashMap=>AbstractMap hashTable=>Dictionary 2.性能:hashMap ...

  3. SQL 统计两个表的数据,按同一日期分组

    思路:把两个表的数据按日期整合到临时表在按日期分组,求和. 例子: SELECT t.dateTime AS '日期',SUM(t.money) AS '表1利息',SUM(t.interest) A ...

  4. 21. Merge Two Sorted Lists —— Python

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  5. 聊一聊log4j2配置文件log4j2.xml

    一.背景 最近由于项目的需要,我们把log4j 1.x的版本全部迁移成log4j 2.x 的版本,那随之而来的slf4j整合log4j的配置(使用Slf4j集成Log4j2构建项目日志系统的完美解决方 ...

  6. js实现返回顶部功能的解决方案

    很多网站上都有返回顶部的效果,主要有如下几种解决方案. 1.纯js,无动画版本 window.scrollTo(x-coord, y-coord); window.scrollTo(0,0); 2.纯 ...

  7. llvm-summary

    llvm 学习总结 Type define int类型 IntegerType::get(mod->getContext(), 32) long类型 IntegerType::get(mod-& ...

  8. 【Java远程debug】

    转自 http://blog.csdn.net/hongchangfirst/article/details/44191925 一.远程debug原理 Java远程调试的原理是两个JVM之间通过deb ...

  9. 组合模式/composite模式/对象结构型模式

    组合模式/composite模式/对象结构型 意图 将对象组合成树形结构以表示"整体--部分"的层次结构.Composite使得用户对单个对象和组合对象的使用具有一致性. 动机 C ...

  10. jq 剪切板

    文章链接 http://www.cnblogs.com/lkxsnow/p/5372665.html http://www.w3cfuns.com/notes/17735/020c2e68a60342 ...