当前位置: 代码迷 >> J2EE >> 在学习hibernate的时候发现异常
  详细解决方案

在学习hibernate的时候发现异常

热度:43   发布时间:2016-04-17 23:12:05.0
在学习hibernate的时候发现错误
我的目录是这样的
SimpleHibUse
       src
           example
               Test.java
                User.java
                User.hbm.xml
           hibernate.cfg.xml


其中hibernate.cfg.xml中的mapping resource是如下内容
<mapping resource="SimpleHibUse/src/example/User.hbm.xml"/>

Test.java里面主要内容
User user = new User();
user.setId(20142103);
user.setUsername("tom");
user.setPassword("123456");

Configuration cfg  = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();//产生数据库的connection
Session session = sf.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();

session.close();
sf.close();

然后产生的错误为
Exception in thread "main" org.hibernate.MappingNotFoundException: resource: SimpleHibUse/src/example/User.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:610)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1635)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1603)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1582)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1556)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1476)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
at example.Test.main(Test.java:20)



------解决思路----------------------
在同一目录下直接<mapping resource="User.hbm.xml"/>就行了吧
你试试按着ctrl 把鼠标放上去看会不会出超链接下划线
------解决思路----------------------
引用:
在同一目录下直接<mapping resource="User.hbm.xml"/>就行了吧
你试试按着ctrl 把鼠标放上去看会不会出超链接下划线



好像不是,你这个是User.hbm.xml在example下, hibernate.cfg.xml在src下?
那就<mapping resource="example/User.hbm.xml"/>
  相关解决方案