开发者

Hibernate DAO doesn't insert row into database MyEclipse

I'm trying to insert a new object into my database. I followed a step-by-step tutorial but it seems it doesn't work for me. In the tutorial there was the following line :

Transaction tx = dao.GetSession().beginTransaction();

The GetSession doesn't pop up, i get the error "GetSession() is not visible from DaoHibernateSupport". I replaced the line with the following :

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

but then i got a null Exception on the currentSession.

I read online and added the current_session_context property, set as "thread".

Everything seems to work now, i don 't get any Exception but still no rows are inserted into my MySql database. The table is InnoDB.

Here is my final code:

         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();
  • So no exceptions raised here, but when i access the database there are no rows in the table.

    Can you please help me ? Thank you!


You may try

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

instead of

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


I figured it out. When i used reverse engineering in MyEclipse i created a SpringDAO instead of BasicDAO. Now the method getSession() works fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜