hibernate的HibernateSessionFactory

longklong4k 2009-11-08 08:41:32
小弟初学hibernate,有些问题不是很明了!
想请教下。
就是用myelipse完成hibernate设置后,会自动产生这个文件。
那这个文件需要更改么?直接在让与数据库交互的JAVA类把它实例化就可以了?

另外有段代码我不是很明白:
public void findAllCustomers(ServletContext context,OutputStream out) throws Exception{
// Ask for a session using the JDBC information we've configured
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
List customers=session.find("from Customer as c order by c.name asc");
for (Iterator it = customers.iterator(); it.hasNext();) {
printCustomer(context,out,(Customer) it.next());
}

// We're done; make our changes permanent
tx.commit();

}catch (Exception e) {
if (tx != null) {
// Something went wrong; discard all partial changes
tx.rollback();
}
throw e;
} finally {
// No matter what, close the session
session.close();
}
}

中间的 tx = session.beginTransaction();是只开启数据库事物么?可以这样理解么?

...全文
110 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
longklong4k 2009-11-08
  • 打赏
  • 举报
回复
用createQuery封装SQL语句么?
礼拜六 2009-11-08
  • 打赏
  • 举报
回复
没什么要改的

mdqy195905 2009-11-08
  • 打赏
  • 举报
回复
还要类型转换。。。
longklong4k 2009-11-08
  • 打赏
  • 举报
回复
根据这段代码中的
List customers=session.find("from Customer as c order by c.name asc");
的find方法用3.2的hibernate就报错了
youjianbo_han_87 2009-11-08
  • 打赏
  • 举报
回复
是的,其实这些只是封装了一下的把戏,没什么特别的东西,工具生成的类,该改就改。
longklong4k 2009-11-08
  • 打赏
  • 举报
回复
哦!

呵呵,是某书实例代码!

hibernate的版本也是很早2.1
gzbtiantian 2009-11-08
  • 打赏
  • 举报
回复
tx = session.beginTransaction(); 是开启当前session的事物,你这个方法的封装不是太好,

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧