org.hibernate.HibernateException: /hibernate.cfg.xml not found

这两天学习MyEclipse配置Hibernate以及Hibernate的使用,按照视频教程一步步做下来,但是结果却显示org.hibernate.HibernateException: /hibernate.cfg.xml not found(教程里是顺利运行的),上网找了半天,都说是文件放错了地方,我明明在src目录下放了/hibernate.cfg.xml的啊,于是沿着错误路径追踪,在HibernateSessionFactory类下发现这样一段话

 /**
     * Location of hibernate.cfg.xml file.
     * Location should be on the classpath as Hibernate uses  
     * #resourceAsStream style lookup for its configuration file.
     * The default classpath location of the hibernate config file is
     * in the default package. Use #setConfigFile() to update
     * the location of the configuration file for the current session.   
     */


这个/hibernate.cfg.xml前面应该是classpath的路径。那么classpath在哪里呢?于是又上网查,看到这么一段话:


一般在实际应用中,你的hibernate.cfg.xml配置文件并没有直接像导入jar包一样导入到classpath下,而只要放到你的Eclipse建立的工程的src目录下即可,hibernate就能自动找到。这是为什么呢?因为src目录确实不在classpath下面,但因为你的项目进行编译时,Eclipse会将你的源文件编译后存在一个叫bin的目录下,同时你的配置文件也相应的复制过去了,而这个bin目录属于classpath的一部分,所以Hibernat就能自动找到你的配置文件了。你可以打开你的工程下的.classpath文件,里面有一行:<classpathentry kind="output" path="bin"/>。你也可以把bin目录下的hibernate.cfg.xml文件删除然后再运行一下原来的程序,它会抛出如下 的一个异常:Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found。


于是打开bin目录,奇怪的是里面只有.class文件复制了进去,没见到xml配置文件呢。。。于是手动把hibernate.cfg.xml复制了进去,再运行一下,这次又找不到user.hbm.xml了尴尬,再把这个文件复制到bin目录下的相应位置,运行一下,终于成功了。不过为什么xml没有被复制到bin目录下呢。。。这问题还没有解决,总之先马克一下了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值