这个问题困扰了我好久,一开始不知道怎么回事,单元测试测试了sessionfactory,transaction,action都没有问题,但是在重启tomcat的时候又报错说在hibernate.cfg.xml里面error creating bean with name "sessionfactory" 问题,关键是当你再重启一遍的时候奇怪的是又可以reload了,这是个非常奇怪的问题,当时没有在意。几天过后有一天写程序写到很晚已经断网了,这是部署项目怎么都是报这个错,所以认为是要联网才能部署。
问题原因:<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
问题在加粗部分,本来hibernate会去找jar包里的这个hibernate-configuration-3.0.dtd,但是如果你的jar包版本不同可能就不会是这个"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd";而是别的比如:"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd";
解决方法1:
将加粗部分改成"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
即:<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
解决方法2:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"hibernate-configuration-3.0.dtd">
之前我的是<!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
hibernate配置文件会去你的classpath中找hibernate-configuration-3.0.dtd这个文件,如果找不到就在外网缓存,所以要在联网的时候才能部署成功。
解决办法:把hibernate-configuration-3.0.dtd这个文件下载下来放到你tomcat的bin目录下面。完成!!