最近用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
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
解决方案 »
- 【求助】SSH 问题java.lang.RuntimeException: Invalid action class configuration
- HQL语句:mysql中怎么比较日期
- 一个关于数据库连接的问题
- Spring+Hibernate新人问题
- 12K - 14K高薪招骋Java/J2EE架构师工程师
- 救命 cvsnt(login 出错:cvs [login aborted]: Error reading from server 192.168.1.100: -1: Unknown error)
- 求助
- XML文件解析失败:属性前要求有空白。
- 抱歉,抱歉,,问个很菜的问题!大家不要笑哈!送100分!
- 创建自定义标签时出现问题,求帮忙帮忙
- 请问下大家都用什么软件管理软件开发中的文档资料
- SOS list比较问题
hql方言只有在连接可用的情况下才能设置,
你调整下hibernate.dialect=org.hibernate.dialect.SQLServerDialect 位置看看
放到hibernate.show_sql=true 下
http://www.duduwolf.com/wiki/2007/677.html
放到最上面看看
我觉得有可能是你之前的某个connection没有关闭引起的。
<!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>
config.configure();
SessionFactory sessionfactory=config.buildSessionFactory();
少了一句 <property name="current_session_context_class">thread</property>
能帮个忙解决下吗?谢谢!
原来是少了这一句 config.configure();