我試圖插入一個新的對象到我的數據庫。我按照一步一步的教程,但似乎它不適合我。在教程中有以下行:休眠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();
所以沒有在這裏引發的異常,但是當我訪問數據庫有表中沒有行。
你能幫我嗎? 謝謝!
save()做了什麼? – Bozho 2011-05-16 07:29:17
也許問題出在Banner-class,如果你註釋錯了。 – Kai 2011-05-16 08:35:27
@ user714965好吧,我在MyEclipse中使用了Hibernate Reverse Engineering,所以這個類自動創建了。他們不應該是正確的嗎?我看着橫幅課,一切似乎都好。 – 2011-05-16 10:34:04