最近用hibernate 框架写了个小程序  可是出现了个奇怪的问题  ,一开始我是用hibernate.properties当配置文件的,程序完全能运行,没有错误,  我就试一下hibernate.cfg.xml(因为一直用hibernate.properties当配置文件,新鲜就用一下hibernate.cfg.xml当配置文件)当配置文件,结果就出错了,  两个配置文件的内容是一样的  可是不知道原因,请高手指点
hibernate.cfg.xml 文件内容:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
     <property name="show_sql">false</property>
        <property name="connection.username">sa</property>
        <property name="connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=lzc01</property>
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>       
        <property name="connection.password">000</property>
        <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    </session-factory>
</hibernate-configuration>
hibernate.properties配置文件:
hibernate.dialect=org.hibernate.dialect.SQLServerDialect
hibernate.connection.driver_class=com.microsoft.sqlserver.jdbc.SQLServerDriver
hibernate.connection.url=jdbc:sqlserver://localhost:1433;DatabaseName=lzc01
hibernate.connection.username=sa
hibernate.connection.password=000
hibernate.show_sql=true用hibernate.cfg.xml文件出错内容:
Exception in thread "main" java.lang.ExceptionInInitializerError
at cn.Action.JDK01.main(JDK01.java:10)
Caused by: org.hibernate.HibernateException: 'hibernate.dialect' must be set when no Connection avalable
at org.hibernate.dialect.resolver.DialectFactory.buildDialect(DialectFactory.java:107)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:138)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2119)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2115)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1339)
at cn.Action.AllControl.<clinit>(AllControl.java:19)
... 1 more

解决方案 »

  1.   

    Caused by: org.hibernate.HibernateException: 'hibernate.dialect' must be set when no Connection avalable 
    hql方言只有在连接可用的情况下才能设置,
    你调整下hibernate.dialect=org.hibernate.dialect.SQLServerDialect 位置看看
    放到hibernate.show_sql=true 下
      

  2.   

    可能是方言放错位置了
    http://www.duduwolf.com/wiki/2007/677.html
      

  3.   

     <property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver </property> 
    放到最上面看看
      

  4.   

    楼主进hibernate3.jar这个包中找到org.hibernate.dialect这个路径下,看看有没有SQLServerDialect这个class,有时候误把一个字母大写错写成小写,都会出错。
      

  5.   

    你重启下机器试试。重启后别运行别的,先运行这个pg。
    我觉得有可能是你之前的某个connection没有关闭引起的。
      

  6.   

    我把这句 <property name="dialect">org.hibernate.dialect.SQLServerDialect </property> 去掉 错误还是一样的 这句难道是可有可无?
      

  7.   

    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools.                   -->
    <hibernate-configuration><session-factory>
    <property name="connection.username">sa</property>
    <property name="connection.url">
    jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jb_crm_team0
    </property>
    <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
    <property name="myeclipse.connection.profile">
    jb_crm_team0
    </property>
    <property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
    <property name="connection.password"></property></session-factory></hibernate-configuration>
      

  8.   

     Configuration config=new Configuration();
     config.configure();
     SessionFactory sessionfactory=config.buildSessionFactory();
    少了一句 <property name="current_session_context_class">thread</property>
      

  9.   

    楼主,你的问题最后解决了没?我现在也出现与你同样的问题:'hibernate.dialect' must be set when no Connection avalable
    能帮个忙解决下吗?谢谢!
      

  10.   


    原来是少了这一句  config.configure();