Java HQL并检查对象是否为null或对象字段是否具有特定值

Java HQL并检查对象是否为null或对象字段是否具有特定值,java,sql,hibernate,hql,Java,Sql,Hibernate,Hql,我在执行以下查询时遇到问题: from Customer c where c.connectedUserID = 1 and (c.connectedFinancialAnalyst is null or c.connectedFinancialAnalyst.completed = false) 对象c.connectedFinancialAnalyst可以为空。我需要的是找到没有财务分析师的客户,或者财务分析师中的财产具有特定价值 编辑: 在postgres和java中,completed

我在执行以下查询时遇到问题:

from Customer c where c.connectedUserID = 1 and (c.connectedFinancialAnalyst is null or c.connectedFinancialAnalyst.completed = false)
对象c.connectedFinancialAnalyst可以为空。我需要的是找到没有财务分析师的客户,或者财务分析师中的财产具有特定价值

编辑: 在postgres和java中,completed属性为布尔值

由Hibernate生成的输出

select
        customer0_.id as id1_0_,
        customer0_.birthDate as birthDat2_0_,
        customer0_.birthPlace as birthPla3_0_,
        customer0_.city as city4_0_,
        customer0_.email as email5_0_,
        customer0_.fatherName as fatherNa6_0_,
        customer0_.motherName as motherNa7_0_,
        customer0_.name as name8_0_,
        customer0_.pesel as pesel9_0_,
        customer0_.phoneNumber as phoneNu10_0_,
        customer0_.postalCode as postalC11_0_,
        customer0_.secondName as secondN12_0_,
        customer0_.sex as sex13_0_,
        customer0_.street as street14_0_,
        customer0_.surname as surname15_0_,
        customer0_.connectedFinancialAnalyst_id as connect17_0_,
        customer0_.connectedUserID as connect16_0_ 
    from
        Customer customer0_ cross 
    join
        FinancialAnalyst financiala1_ 
    where
        customer0_.connectedFinancialAnalyst_id=financiala1_.id 
        and customer0_.connectedUserID=1 
        and (
            customer0_.connectedFinancialAnalyst_id is null 
            or financiala1_.completed='false'
        )

您需要明确表示要进行左连接

from Customer c 
left join c.connectedFinancialAnalyst as analyst
where c.connectedUserID = 1 and (analyst.completed is null or analyst.completed = false)

您需要明确表示要进行左连接

from Customer c 
left join c.connectedFinancialAnalyst as analyst
where c.connectedUserID = 1 and (analyst.completed is null or analyst.completed = false)

我重新编写了我的代码,使之符合标准,并且每一个东西都工作得很好。我想问题出在左连接上。当我使用条件时,我必须附加Left-Outter-join,如:

Criteria c = session.createCriteria(Customer.class, "c").createAlias("c.connectedFinancialAnalyst", "analyst", JoinType.LEFT_OUTER_JOIN);

也许当我将查询时的左连接改为左外连接时,一切都会很好^^。但现在我的代码似乎更易于用户使用了^ ^

我将代码改写成了标准,所有东西都工作得很好。我想问题出在左连接上。当我使用条件时,我必须附加Left-Outter-join,如:

Criteria c = session.createCriteria(Customer.class, "c").createAlias("c.connectedFinancialAnalyst", "analyst", JoinType.LEFT_OUTER_JOIN);

也许当我将查询时的左连接改为左外连接时,一切都会很好^^。但现在我的代码似乎更方便用户了^^

列已完成是VARCHAR还是bit?PostGresql说它是布尔值。然后它应该是“false”。i、 e用单引号括起来。看看这个,我已经试过了。我认为当我尝试检查未存在的objectColumn Completed上的值时会出现问题,它是VARCHAR还是BIT?PostGresql说它是布尔值。然后它应该是“false”。i、 e用单引号括起来。看看这个,我已经试过了。我认为当我尝试检查不存在的对象的值时会出现问题让我知道哪一个对你有效,我会相应地更新答案。我尝试过这个,但它的输出与以前的相同。我很确定问题出在:“c.connectedFinancialAnalyst.completed为null或c.connectedFinancialAnalyst.completed=false”尝试执行左连接。它返回了预期的行数,现在我必须处理返回的对象,因为它们是其他类型的对象。如果您从客户c中选择了c,那么您将获得客户对象。这不是你想要的吗?让我知道哪一个适合你,我会相应地更新答案。我已经尝试过了,但它的输出与ealier相同。我很确定问题出在:“c.connectedFinancialAnalyst.completed为null或c.connectedFinancialAnalyst.completed=false”尝试执行左连接。它返回了预期的行数,现在我必须处理返回的对象,因为它们是其他类型的对象。如果您从客户c中选择了c,那么您将获得客户对象。这不是你想要的吗?