我在对来自 JPA 查询的数据进行排序时遇到问题。我有一个父类是Variable。例如,它有id、frameId、name等。它有一个子类,它们是Dnp3Variable、ModbusVariable、OpcDaVariable。Dnp3Variable 和 ModbusVariable 有variableId、startAddress等。但 OpcDaVariable 没有startAddress。SQL 连接所有这些变量模型,并通过使用startAddress排序来选择过滤的数据。但是,spring 使用一个模型对其进行排序。它可以是 ModbusVariable、Dnp3Variable 或 OpcDaVariable。因此,结果数据没有被排序。这是方法:
@Query(value = "select var from #{#entityName} var join var.frame fra " +
"join fra.device dev " +
"join dev.connection conn where " +
"(var.projectId = ?#{#variableFilter.projectId} or ?#{#variableFilter.projectId} is null) and " +
"(conn.id = ?#{#variableFilter.connectionId} or ?#{#variableFilter.connectionId} is null) and " +
"(dev.id = ?#{#variableFilter.deviceId} or ?#{#variableFilter.deviceId} is null) and " +
"(fra.id = ?#{#variableFilter.frameId} or ?#{#variableFilter.frameId} is null) and " +
"(var.name = ?#{#variableFilter.name} or ?#{#variableFilter.name} is '') and" +
"(?#{#variableFilter.nameIn} is null or upper(var.name) like %?#{#variableFilter.nameIn}% ) and" +
"(?#{#variableFilter.dscIn} is null or upper(var.dsc) like %?#{#variableFilter.dscIn}%)")
Page<TVariable> findVariablesByFilter(@Param("variableFilter") VariableFilter variableFilter,
@Param("pageable") Pageable pageable);
这是sql日志:
select
var
from
Variable var
join
var.frame fra
join
fra.device dev
join
dev.connection conn
where
(
var.projectId = ?1
or ?2 is null
)
and (
conn.id = ?3
or ?4 is null
)
and (
dev.id = ?5
or ?6 is null
)
and (
fra.id = ?7
or ?8 is null
)
and (
var.name = ?9
or ?10 is ''
)
and(
?11 is null
or upper(var.name) like ?12
)
and(
?13 is null
or upper(var.dsc) like ?14
)
order by
var.startAddress asc */ select
variable0_.variable_id as variable1_72_,
variable0_.insert_user as insert_u2_72_,
variable0_.insert_dttm as insert_d3_72_,
variable0_.version_user as version_4_72_,
variable0_.version_dttm as version_5_72_,
variable0_.code as code6_72_,
variable0_.dsc as dsc7_72_,
variable0_.eng_full_scale as eng_full8_72_,
variable0_.eng_zero_scale as eng_zero9_72_,
variable0_.fractional_digit_count as fractio10_72_,
variable0_.frame_id as frame_i11_72_,
variable0_.active_flag as active_12_72_,
variable0_.log_expression_id as log_exp14_72_,
variable0_.log_expression as log_exp13_72_,
variable0_.log_period as log_per15_72_,
variable0_.log_threshold as log_thr16_72_,
variable0_.log_type as log_typ17_72_,
variable0_.name as name18_72_,
variable0_.project_id as project25_72_,
variable0_.raw_full_scale as raw_ful19_72_,
variable0_.raw_zero_scale as raw_zer20_72_,
variable0_.unit as unit21_72_,
variable0_.value_expression_id as value_e23_72_,
variable0_.value_expression as value_e22_72_,
variable0_.value_expression_type as value_e24_72_,
variable0_1_.type as type1_44_,
variable0_2_.start_address as start_ad1_28_,
variable0_3_.dead_band as dead_ban1_21_,
variable0_3_.event_variation as event_va2_21_,
variable0_3_.point_class as point_cl3_21_,
variable0_3_.start_address as start_ad4_21_,
variable0_3_.static_variation as static_v5_21_,
variable0_4_.bit_offset as bit_offs1_61_,
variable0_4_.start_address as start_ad2_61_,
variable0_4_.type as type3_61_,
variable0_5_.bit_offset as bit_offs5_38_,
variable0_5_.byte_swap_flag as byte_swa1_38_,
variable0_5_.start_address as start_ad2_38_,
variable0_5_.type as type3_38_,
variable0_5_.word_swap_flag as word_swa4_38_,
case
when variable0_1_.variable_id is not null then 1
when variable0_2_.variable_id is not null then 2
when variable0_3_.variable_id is not null then 3
when variable0_4_.variable_id is not null then 4
when variable0_5_.variable_id is not null then 5
when variable0_.variable_id is not null then 0
end as clazz_
from
inscada.variable variable0_
left outer join
inscada.opc_da_variable variable0_1_
on variable0_.variable_id=variable0_1_.variable_id
left outer join
inscada.iec104_variable variable0_2_
on variable0_.variable_id=variable0_2_.variable_id
left outer join
inscada.dnp3_variable variable0_3_
on variable0_.variable_id=variable0_3_.variable_id
left outer join
inscada.s7_variable variable0_4_
on variable0_.variable_id=variable0_4_.variable_id
left outer join
inscada.modbus_variable variable0_5_
on variable0_.variable_id=variable0_5_.variable_id
inner join
inscada.frame frame1_
on variable0_.frame_id=frame1_.frame_id
inner join
inscada.device device2_
on frame1_.device_id=device2_.device_id
inner join
inscada.connection connection3_
on device2_.conn_id=connection3_.conn_id
where
(
variable0_.project_id is not null
)
and (
variable0_.project_id=?
or ? is null
)
and (
connection3_.conn_id=?
or ? is null
)
and (
device2_.device_id=?
or ? is null
)
and (
frame1_.frame_id=?
or ? is null
)
and (
variable0_.name=?
or ?=''
)
and (
? is null
or upper(variable0_.name) like ?
)
and (
? is null
or upper(variable0_.dsc) like ?
)
order by
variable0_2_.start_address asc limit ?