使用hibernate3.3连接mysql问题

yhr7289864 2009-11-30 11:09:07
hibernate包导入,vo和与数据库映射文件,配置文件写了
<?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="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/oa
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">mysql</property>
<property name="connection.password">pass</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="connection.autocommit">true</property>

<mapping resource="com/oa/xml/hibernate/Notice.hbm.xml" />

</session-factory>
</hibernate-configuration>

测试代码如下:
Configuration cfg = new Configuration();
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();
出现异常如下:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/F:/OA%e9%a1%b9%e7%9b%ae/OA/lib/slf4j-nop-1.5.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/F:/OA%e9%a1%b9%e7%9b%ae/OA/lib/slf4j-simple-1.5.8.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
Exception in thread "main" org.hibernate.HibernateException: Hibernate Dialect must be explicitly set
at org.hibernate.dialect.DialectFactory.determineDialect(DialectFactory.java:80)
at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:62)
at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:460)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:155)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2101)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1325)
at com.oa.test.Test.main(Test.java:11)


...全文
99 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yhr7289864 2009-11-30
  • 打赏
  • 举报
回复
我放在classpath下,hibernate的sessionFactory由spring管理,然后我用hibernate包下sessionFactory和session
Landor2004 2009-11-30
  • 打赏
  • 举报
回复
也许是没有找到这个配置文件,确认文件是否放在了classpath下
wzju64676266 2009-11-30
  • 打赏
  • 举报
回复
public Configuration configure()
throws HibernateException
{
configure("/hibernate.cfg.xml");
return this;
}

public Configuration configure(String s)
throws HibernateException
{
log.info("configuring from resource: " + s);
InputStream inputstream = getConfigurationInputStream(s);
return doConfigure(inputstream, s);
}

调用configure()方法才会加载hibernate配置文件
wzju64676266 2009-11-30
  • 打赏
  • 举报
回复

Configuration cfg = new Configuration();
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();


Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
Landor2004 2009-11-30
  • 打赏
  • 举报
回复
Configuration cfg = new Configuration();
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();

用spring管理还这么创建session ?

50,526

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧