[英]Delete all objects from database in hibernate Spring java
I want to delete all those rows from xyz table where id = 1 using hibernate spring. 我想使用hibernate spring从xyz表中删除所有那些id = 1的行。
I have tried following code but its not giving error but not deleting rows - 我试过以下代码,但它没有给出错误但没有删除行 -
Session session = (Session) getEm().getDelegate();
String sql ="Delete from xyz where id=:id" ;
SQLQuery query = session.createSQLQuery(sql);
query.setParameter("id", "1");
int flg = query.executeUpdate();
Can you please help me to delete all rows using hibernate query. 你能帮我用hibernate查询删除所有行吗?
Try 尝试
query.setParameter("id", Long.valueOf(1));
if your entity is of type Long (which ideally should be). 如果您的实体是Long类型(理想情况下应该是)。
Reference: http://www.codejava.net/frameworks/hibernate/hibernate-basics-3-ways-to-delete-an-entity-from-the-datastore 参考: http : //www.codejava.net/frameworks/hibernate/hibernate-basics-3-ways-to-delete-an-entity-from-the-datastore
Note: The link is just for your reference. 注意:该链接仅供您参考。
public void deleteById(Class clazz,Integer id) {
String hql = "delete " + clazz.getName() + " where id = :id";
Query q = session.createQuery(hql).setParameter("id", id);
q.executeUpdate();
}
Try wrapping your code within a transaction like this: 尝试在这样的事务中包装代码:
Session session = (Session) getEm().getDelegate();
Transaction tx = session.beginTransaction();
String sql ="Delete from xyz where id=:id" ;
SQLQuery query = session.createSQLQuery(sql);
query.setParameter("id", "1");
int flg = query.executeUpdate();
tx.commit();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.