Syd*_*ney 2 java hibernate hql
我有一个HQL查询的问题.我希望将管理性别设置为"M"或没有管理性的所有PID(在Java中将值设置为null).
PID.class
@Entity
@Table(name = "PatientIdentification")
public class PID {
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "administrativeSex", referencedColumnName = "is_id")
private IS administrativeSex;
...
}
Run Code Online (Sandbox Code Playgroud)
IS.class
@Entity
@Table(name = "CodedValueForUserDefinedTables")
public class IS {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "is_id")
private Integer id;
private String value;
...
}
Run Code Online (Sandbox Code Playgroud)
HQL查询
from PID where administrativeSex is null or administrativeSex.value = 'M'
Run Code Online (Sandbox Code Playgroud)
生成的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'
)
Run Code Online (Sandbox Code Playgroud)
该查询仅返回管理性别设置为"M"的PID.它缺少一个没有行政性的人.如果查看SQL查询,这是正常的.如何更正我的HQL查询?
hibernate参考手册写道:
HQL支持两种形式的关联连接:隐式和显式.
上一节中显示的查询都使用显式形式,即在from子句中显式使用join关键字的位置.这是推荐的表格.
隐式表单不使用join关键字.相反,使用点符号"解除引用"关联.隐式连接可以出现在任何HQL子句中.隐式连接导致生成的SQL语句中的内部联接.
由于内部联接过滤掉了连接条件不匹配的行,因此您缺少未知性别的人.您将需要一个左外连接,您必须要求明确:
from PID pid
left outer join pid.administrativeSex as sex
where pid.administrativeSex is null or sex.value = 'M'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5128 次 |
| 最近记录: |