Hibernate DAO 不会将行插入数据库 MyEclipse
我正在尝试将一个新对象插入我的数据库。我按照分步教程进行操作,但似乎它对我不起作用。在教程中有以下行:
Transaction tx = dao.GetSession().beginTransaction();
GetSession 没有弹出,我收到错误“GetSession() 在 DaoHibernateSupport 中不可见”。 我用以下内容替换了该行:
Transaction tx = dao.getSessionFactory().getCurrentSession().beginTransaction();
但随后我在 currentSession 上得到了 null 异常。
我在网上阅读并添加了current_session_context属性
,设置为“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();
所以这里没有引发异常,但是当我访问数据库时,表中没有行。
你能帮我一下吗? 谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以尝试
代替
You may try
instead of
我想通了。当我在 MyEclipse 中使用逆向工程时,我创建了 SpringDAO 而不是 BasicDAO。现在 getSession() 方法可以正常工作了。
I figured it out. When i used reverse engineering in MyEclipse i created a SpringDAO instead of BasicDAO. Now the method getSession() works fine.