java - 如果一列中有空值,则无法获取行 hibernate hql

标签 java mysql hibernate orm hql

我在搜索操作中遇到困难。如果一行的列中有任何 null 值,则该行不会出现。我尝试在 hql 中使用 is not null,但无法获取值。我已经编写了这样的 hql 查询。它什么也没返回。我无法理解它是如何工作的。请告诉我这个问题的解决方案。我正在使用mysql

session.createQuery("FROM Employee e WHERE (e.company is not null and e.company.name = :p1) or e.name = :p1").setParameter("p1", str)

我的@Entity类是这些:

@Entity
public class Employee implements Serializable {

    @Id
    @GeneratedValue
    private Long id;
    private String name;
    private float salary;

    @OneToOne(cascade = CascadeType.ALL)
    private Company company;

data of <code>Employee</code> in the Database

@Entity
public class Company implements Serializable {

    @Id
    @GeneratedValue
    private Long id;
    private String name;
    private String Location;

data of the <code>Company</code> table

最佳答案

试试这个

session.createQuery("FROM Employee e left join e.company c WHERE c.name = :p1 or e.name = :p1").setParameter("p1", "uday11")

当您在查询中使用 e.company 时,它会被转换为内连接,这就是它无法按预期工作的原因。

关于java - 如果一列中有空值,则无法获取行 hibernate hql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28989265/

相关文章:

java - 如何从 OSM Tiles 在 Java 应用程序中创建 map ?

php - 依赖mysql中的错误代码1062

java - 组织.hibernate.MappingException : Repeated column in mapping for entity

java - hibernate : Using AND combined with OR in a single query

java - 使用 javax.mail 发送电子邮件 : the newer version of the library brings unexpected errors

java - 打开失败 EACCES(权限被拒绝)

java - 每个测试为 Jenkins 上的 JUnit 测试套件分离了 stdout/stderr

sql - MYSQL 对同一类别进行多项选择?

python - 如何在没有互联网访问的情况下为 python 安装 MySQL

hibernate - Grails CriteriaBuilder 添加限制