Hibernate 对数据库操作(菜鸟问题)

haijun1130 2007-09-14 12:09:00
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

String hqlUpdate = "update Customer set name = :newName where name = :oldName";
int updatedEntities = s.createQuery( hqlUpdate )
.setString( "newName", newName )
.setString( "oldName", oldName )
.executeUpdate();
tx.commit();
session.close();

请问 updatedEntities = s.createQuery( hqlUpdate ) 中 s 是哪来的啊
...全文
157 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyy_13579 2007-09-15
  • 打赏
  • 举报
回复
来迟了,居然被大家瓜分了。
第一,语句用错了,应该是saveOrUpdate() 。
第二,那里应该是session不是s啊。
haijun1130 2007-09-14
  • 打赏
  • 举报
回复
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

String hqlUpdate = "update Customer set name = :newName where name = :oldName";
int updatedEntities = session .createQuery( hqlUpdate ) //我将 session 代替 s 时编译没问题 但运行没成功.知道是什么原因吗?
.setString( "newName", newName )
.setString( "oldName", oldName )
.executeUpdate();
tx.commit();
session.close();
zjx8342 2007-09-14
  • 打赏
  • 举报
回复
s是你在配置文件中用s代替了session的吧
locoasta 2007-09-14
  • 打赏
  • 举报
回复
s就是 Session session = sessionFactory.openSession();
定义的session LZ的定义有问题吧。。。
locoasta 2007-09-14
  • 打赏
  • 举报
回复
补上面的 saveOrUpdate(pojo)
参数是pojo 你只需要传递个pojo进来就行了

Pojo pojo = new Pojo();
pojo.setnewName(newName);
pojo.setoldName(oldName);

如果oldname存在就update 不存在就insert

locoasta 2007-09-14
  • 打赏
  • 举报
回复
String hqlUpdate = "update Customer set name = :newName where name = :oldName";
int updatedEntities = session .createQuery( hqlUpdate ) //我将 session 代替 s 时
有问题 session .createQuery是查询的方法
如果是update
用 saveOrUpdate() 吧
forestking_xx 2007-09-14
  • 打赏
  • 举报
回复
运行不成功是什么意思啊?又错误吗?还是和预想的不一样?有错误把错误信息贴出来可能更清楚点.
「已注销」 2007-09-14
  • 打赏
  • 举报
回复
createQuery的参数应该放查询语句吧

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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