【转】The user must supply a JDBC connection

Hibernate配置陷阱
本文解析了Hibernate配置中常见的错误,特别是由于未正确初始化Configuration对象而导致的问题。介绍了如何避免这些错误,并给出了正确的代码示例。

The user must supply a JDBC connection

hibernate的一个程序就遇到这个错误,google 了一下找到了问题的解决方法。

// *********************************************** //

This is another mistake that's easy to do accidentally, especially by newbies.



If you get a Hibernate error like the one shown above and you're dead sure that your hibernate.cfg.xml is in the classpath, make sure that your initialisation code looks like this:

config = new Configuration();
sessions = config.configure().buildSessionFactory();
// DON'T DO THIS!!!
// sessions = config.buildSessionFactory();

Remember to call the method of when using hibernate.cfg.xml, otherwise this config file won't be picked up by Hibernate.

 

原因:默认 new Configuration(); 会加载默认的 属性配置文件, 即hibernate.properties

若是用的基于XML 的配置文件, 则要调用 configure() 方法来装载配置信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值