如何在 HQL 中使用 NOT LIKE?

how to use NOT LIKE in HQL?

我有一个实体如下

public class Employee implements Serializable {


@Id
@Column(name = "EMPSEQ")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long empSeq;
@Column(name = "EMPID")
private String empId;
@Column(name = "WINDOWSLOGINID")
private String logInId;

// assume respective getter and setter methods
}

我想查询 logInId 不以“5”开头的所有行

我尝试了以下代码:

query = session.createQuery("select * from Employee e where e.logInId not like 5%");

以上代码无效。在 HQL

中使用 NOT LIKE 的正确方法是什么

您的查询有一个错误:

query = session.createQuery("select * from Employee e where e.logInId not like 5%");

成为:

query = session.createQuery("select * from Employee e where e.logInId not like '5%'");

e.logInId是字符串,所以你必须引用你的条件5%。

您也可以使用 Hibernate Criteria。

JPA EntityManager 具有 unwrap() 方法,它将 return 会话。

  Session session = getEntityManager().unwrap(Session.class);
  Criteria criteria = session.createCriteria(Pojo.class); 
  criteria.add(Restrictions.not(Restrictions.like("loginId","5%")));
  List<Pojo> list=criteria.list();
   if(null!=list && list.size()> 0){
        return list.get(0);
    }
    return null;