2011-05-16 79 views
1

我試圖插入一個新的對象到我的數據庫。我按照一步一步的教程,但似乎它不適合我。在教程中有以下行:休眠DAO不插入行到數據庫MyEclipse

事務tx = dao.GetSession()。beginTransaction();

GetSession不會彈出,我得到錯誤「GetSession()不可見從DaoHibernateSupport」。 我替換爲以下行:

Transaction tx = dao.getSessionFactory().getCurrentSession().beginTransaction(); 

但後來我上了currentSession空例外。

我在線閱讀並添加了current_session_context property,設置爲「thread」。

一切似乎現在工作,我沒有得到任何異常,但仍然沒有行插入到我的MySql數據庫。該表是InnoDB。

這是我的最終代碼:

  Banner banner = new Banner(); 

    banner.setUrl(url); 

    banner.setCategorie(categorie); 

    banner.setCuvinteCheie(cuvinte_cheie); 

    banner.setMaxCpc(cpc); 

    banner.setPath(cale); 

    banner.setPaththumb(caleThumb); 

    banner.setAdvertiserId(Integer.parseInt(session.getAttribute("UserID").toString())); 

    BannerDAO dao = new BannerDAO(); 

    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 

    dao.setSessionFactory(sessionFactory); 

    Transaction tx = dao.getSessionFactory().getCurrentSession().beginTransaction(); 

    dao.save(banner); 

    tx.commit(); 

    dao.getSessionFactory().getCurrentSession().close(); 
  • 所以沒有在這裏引發的異常,但是當我訪問數據庫有表中沒有行。

    你能幫我嗎? 謝謝!

+0

save()做了什麼? – Bozho 2011-05-16 07:29:17

+0

也許問題出在Banner-class,如果你註釋錯了。 – Kai 2011-05-16 08:35:27

+0

@ user714965好吧,我在MyEclipse中使用了Hibernate Reverse Engineering,所以這個類自動創建了。他們不應該是正確的嗎?我看着橫幅課,一切似乎都好。 – 2011-05-16 10:34:04

回答

1

我想通了。當我在MyEclipse中使用逆向工程時,我創建了一個SpringDAO而不是BasicDAO。現在getSession()方法可以正常工作。

1

您可以嘗試

Transaction tx = dao.getSessionFactory().openSession().beginTransaction(); 

,而不是

Transaction tx = dao.getSessionFactory().getCurrentSession().beginTransaction(); 
+0

我試過了。一樣。 – 2011-05-16 10:35:35

+0

你能告訴我執行的SQL查詢嗎? – RAS 2011-05-16 12:37:25

+0

你是什麼意思?我沒有寫任何查詢。 – 2011-05-16 19:51:09