Hibernate delete by criteria error

384 views Asked by At

I have an Entity (persisted on mongodb) called SourceCondition with property workflowID and I want to delete all SourceCondition objects with a particular workflowID.

The entity is:

@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;
    }

}

The query I execute is:

        Session s = HibernateUtil.getSessionFactory().openSession();
        Query query = s.createQuery("delete from SourceCondition where workflowID = :wid");
        query.setParameter("wid", "sampleID");           
        int result = query.executeUpdate();

I receive the following error: Syntax error in query: [delete from com.backend.Models.Source.SourceCondition where workflowID = :wid]

I also tried with:

        Query query = s.createQuery("delete SourceCondition where workflowID = :wid");
        query.setParameter("wid", "sampleID");           
        int result = query.executeUpdate();

but I receive the same error.

=====================

EDIT

I bypass the problem with:

        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();
        }

It is not the best way to achieve deletion, but it works at the moment.

1

There are 1 answers

1
acanby On

You need to give the SourceCondition a variable name:

delete from SourceCondition sc where sc.workflowID = :wid