2012-02-29 66 views
1

我学习hibernate,AFAIK你可以使用xml或注解,我喜欢注释更多,所以试试这种方法。休眠在netbeans新手帮助

  1. 自动创建的hibernate.cfg.xml(Netbeans的向导)
  2. 自动创建的逆向工程的xml
  3. 自动创建HibernateUtil.java
  4. 自动创建POJO类与注释

enter image description here

代码:

SessionFactory session = HibernateUtil.getSessionFactory(); 
    CallInfo ci = new CallInfo(1234567); //POJO class 
    session.getCurrentSession().save(ci); 

和hibernate仍然想要xml,为什么?

Exception in thread "main" java.lang.ExceptionInInitializerError 
at ru.asteros.sochi.feedback.util.HibernateUtil.<clinit>(HibernateUtil.java:28) 
at ru.asteros.sochi.feedback.SochiFeedback.main(SochiFeedback.java:52) 
Caused by: org.hibernate.MappingNotFoundException: resource: ru/asteros/sochi/feedback/CallInfo.hbm.xml not found 
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:563) 
+0

检查生成的'HibernateUtil',并确保它是使用'AnnotationConfiguration'而不是'Configuration'。 – Perception 2012-02-29 11:59:02

回答

1

可能CallInfo.hbm.xmlhibernate.cfg.hbm被引用和不在有效类路径

+0

你是对的,它在那里..所以我已经删除了该行的XML,现在Java说: '线程中的异常“main”org.hibernate.HibernateException:没有配置CurrentSessionContext! \t在org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:542) \t在ru.asteros.sochi.feedback.SochiFeedback.main(SochiFeedback.java:54)' – VextoR 2012-02-29 11:56:33

+1

看到,HTTP://计算器。 COM /问题/ 7281045 /为什么-DO-I-GET-ORG-休眠-HibernateException的-NO-currentsessioncontext-configur – 2012-02-29 12:02:28