如果一列休眠 hql 中有空值,则无法获取该行

Not able to get the row if there is null value in one column hibernate 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;

@Entity
public class Company implements Serializable {

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

试试这个

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

当您在查询中使用 e.company 时,它会被转换为 inner join,这就是它未按预期工作的原因。