- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 HQL 查询有问题。我想要获取所有管理性别设置为“M”或没有管理性别(在 Java 中该值设置为 null)的 PID。
PID.class
@Entity
@Table(name = "PatientIdentification")
public class PID {
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "administrativeSex", referencedColumnName = "is_id")
private IS administrativeSex;
...
}
IS.class
@Entity
@Table(name = "CodedValueForUserDefinedTables")
public class IS {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "is_id")
private Integer id;
private String value;
...
}
HQL 查询
from PID where administrativeSex is null or administrativeSex.value = 'M'
生成的SQL
select
pid0_.pid_id as pid1_84_,
pid0_.administrativeSex as adminis11_84_,
pid0_.birthOrder as birthOrder84_,
pid0_.birthPlace as birthPlace84_,
pid0_.citizenship as citizen12_84_,
pid0_.countyCode as countyCode84_,
pid0_.dateTimeOfBirth as dateTim19_84_,
pid0_.driverLicenseNumber as driverL22_84_,
pid0_.maritalStatus as maritalS8_84_,
pid0_.multipleBirthIndicator as multiple4_84_,
pid0_.nationality as nationa17_84_,
pid0_.owner_id as owner9_84_,
pid0_.patientAccountNumber as patientA6_84_,
pid0_.patientDeathDateAndTime as patient16_84_,
pid0_.patientDeathIndicator as patient18_84_,
pid0_.patientId as patientId84_,
pid0_.primaryLanguage as primaryL7_84_,
pid0_.race as race84_,
pid0_.religion as religion84_,
pid0_.setId as setId84_,
pid0_.ssnNumber as ssnNumber84_,
pid0_.veteransMilitaryStatus as veterans2_84_
from
PatientIdentification pid0_,
CodedValueForUserDefinedTables is1_
where
pid0_.administrativeSex=is1_.is_id
and (
pid0_.administrativeSex is null
or is1_.value='M'
)
查询仅返回管理性别设置为“M”的PID。它缺少一个没有行政性别的人。如果你查看SQL查询,这是正常的。如何更正我的 HQL 查询?
最佳答案
Hibernate 引用手册 writes :
HQL supports two forms of association joining: implicit and explicit.
The queries shown in the previous section all use the explicit form, that is, where the join keyword is explicitly used in the from clause. This is the recommended form.
The implicit form does not use the join keyword. Instead, the associations are "dereferenced" using dot-notation. implicit joins can appear in any of the HQL clauses. implicit join result in inner joins in the resulting SQL statement.
由于内部联接会过滤掉联接条件不匹配的那些行,因此您会丢失未知性别的人员。您将需要一个左外连接,您必须请求 explicity :
from PID pid
left outer join pid.administrativeSex as sex
where pid.administrativeSex is null or sex.value = 'M'
关于java - Hibernate HQL 与 IS NULL 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3400772/
我是一名优秀的程序员,十分优秀!