我有一个名为 SourceCondition 的实体(保留在 mongodb 上),其属性为 workflowID,我想删除所有具有特定 workflowID 的 SourceCondition 对象。
实体是:
@Entity
@Table(name="source_conditions")
public class SourceCondition {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@TableGenerator(
name = "source_conditions"
)
private ObjectId id;
public String getId() { return id.toString(); }
public void setId(ObjectId id) { this.id = id; }
@Column(name="workflowID")
private String workflowID;
public SourceCondition() {}
public String getWorkflowID() {
return workflowID;
}
public void setWorkflowID(String workflowID) {
this.workflowID = workflowID;
}
}
我执行的查询是:
Session s = HibernateUtil.getSessionFactory().openSession();
Query query = s.createQuery("delete from SourceCondition where workflowID = :wid");
query.setParameter("wid", "sampleID");
int result = query.executeUpdate();
我收到以下错误: 查询中的语法错误:[delete from com.backend.Models.Source.SourceCondition where workflowID = :wid]
我也尝试过:
Query query = s.createQuery("delete SourceCondition where workflowID = :wid");
query.setParameter("wid", "sampleID");
int result = query.executeUpdate();
但我收到同样的错误。
======================
编辑
我绕过了这个问题:
Query query1 = s.createQuery("from SourceCondition sc where sc.workflowID = :wid");
query1.setParameter("wid", "sampleID");
List l1 = query1.list();
Iterator<?> it1 = l1.iterator();
while (it1.hasNext())
{
SourceCondition sc = (SourceCondition) it1.next();
s.delete(sc);
s.flush();
}
这不是实现删除的最佳方法,但它目前有效。