关于hibernate的HQL简单查询语句

来源:百度知道 编辑:UC知道 时间:2024/05/05 14:33:04
String hql="delete Comment where id=?";
这样写有错么?
这种方法呢?
Comment comment=new Comment();
comment.setId(id);
session.delete(comment);
comment的其它字段为空能做删除么? 我的id为主键的

首先,hql是做查询用得,不能用来做其他操作。
再有,delete方法是用来删除对象的,这个对象不能是new的,最好是查询出来的,比如comment=get(Comment.class,id);这样就可以删除了。这和hibernte的缓存机制有关系。

第一个加from就可以
第二个可以