【问题标题】:how to use NOT LIKE in HQL?如何在 HQL 中使用 NOT LIKE?
【发布时间】:2017-05-17 21:30:44
【问题描述】:

我有一个实体如下

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的正确方法是什么

【问题讨论】:

  • 不应该是单引号中的模式(5%)吗?比如“5%”?

标签: java hibernate hql


【解决方案1】:

在您的查询中有一个错误:

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%。

【讨论】:

    【解决方案2】:

    您也可以使用 Hibernate Criteria。

    JPA EntityManger 具有 unwrap() 方法,该方法将返回会话。

      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;
    

    【讨论】:

      猜你喜欢
      • 2020-05-24
      • 1970-01-01
      • 1970-01-01
      • 2015-08-23
      • 2021-03-16
      • 1970-01-01
      • 1970-01-01
      • 2014-03-19
      • 2011-06-13
      相关资源
      最近更新 更多