2012-02-29 70 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