使用HQL中的where子句删除查询

时间:2015-07-24 14:05:13

标签: java mysql hql

我是hql的新手,并尝试在基于电子邮件的匹配情况下删除整行。

以下是我的尝试。

我仍然遇到持久性异常。

public void unsubscribeEmailList(EmailListDto dataList) {
    EmailList e =new EmailList(dataList);
    Query q =sessionFactory.getCurrentSession().createQuery("delete from EmailList where email=:e");
q.setParameter("e", dataList);

    int i=q.executeUpdate();
System.out.println(i);
    }

类EmailList是

@Entity
@Table(name = "email_list")
public class EmailList implements Serializable {



private static final long serialVersionUID = 1L;
private Integer           id;
private String            email;


public EmailList(EmailListDto dto)
{
    this.email=dto.getEmail();

}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}
 @Column(name = "email", nullable = false)
 public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}

}

3 个答案:

答案 0 :(得分:1)

试试这个:

q.setParameter("e", e.getEmail());

答案 1 :(得分:0)

 public void unsubscribeEmailList(EmailListDto dataList) {
        EmailList e =new EmailList(dataList);
        Query q =sessionFactory.getCurrentSession().createQuery("delete from EmailList where email = :e");
    q.setParameter("e", e.egetEmail()  );

        int i=q.executeUpdate();
    System.out.println(i);
        }

答案 2 :(得分:0)

...
@NamedQueries(
        {
                @NamedQuery(name = "FindAllJobsEngineersParticipateInInterval",
                            query = "SELECT je.id FROM Stage1JobEngineer AS je " +
                                    "JOIN je.job AS s1j " +
                                    "WHERE "
                                    + " :absenceStartDateTime <= :absenceEndDateTime "
                                    + " AND :absenceStartDateTime <= s1j.endTime AND :absenceEndDateTime >= s1j.startTime " +
                                    "   AND je.engineer.id IN (:engineerIds) ")
        })

@Entity
@Table(name = "ho_stage1_job_has_engineers")
public class Stage1JobEngineer extends CreatedByUserEntity implements Serializable {
...


...
// dao service call : 

Query query = getSession().getNamedQuery( "FindAllJobsEngineersParticipateInInterval" );
        query.setParameter( "absenceStartDateTime", startDate )
             .setParameter( "absenceEndDateTime", endDate )
             .setParameterList( "engineerIds", Arrays.asList(1L, 2L, 33L) );

        List<Long> queryResult = query.list();
        List<Long> result = queryRes != null ? queryResult : new ArrayList<>();

...