【发布时间】:2015-08-28 17:52:24
【问题描述】:
我正在尝试编写一个休眠查询,我正在使用 StringBuilder 并为 ce、pe 和两者设置 3 个单独的字符串集。如何将它添加到我的休眠查询中?我想将它添加到我添加评论以“在此处添加标志”的位置。我的标志类型是枚举“Y”或“N”。
StringBuffer queryByCos4Allocation = new StringBuffer();
Flag ceIngress = null;
Flag ceEgress = null;
Flag peIngress = null;
Flag peEgress = null;
String ce = null;
String pe = null;
String both = null;
if (Flag.Y.equals(ceIngress) || Flag.Y.equals(ceEgress)){
ce = " AND (CE_INGRESS_FLAG = 'Y' OR CE_EGRESS_FLAG = 'Y') "
+ " AND (PE_INGRESS_FLAG = 'N' OR PE_EGRESS_FLAG = 'N') " ;
}if (Flag.Y.equals(peIngress) || Flag.Y.equals(peEgress)){
pe = " AND (CE_INGRESS_FLAG = 'N' OR CE_EGRESS_FLAG = 'N') "
+ " AND (PE_INGRESS_FLAG = 'Y' OR PE_EGRESS_FLAG = 'Y') " ;
}if (ce == pe){
both = " AND (CE_INGRESS_FLAG = 'Y' OR CE_EGRESS_FLAG = 'Y') "
+ " AND (PE_INGRESS_FLAG = 'Y' OR PE_EGRESS_FLAG = 'Y') " ;
}
queryByCos4Allocation.append(" SELECT * FROM TRAFFIC_PROFILE " ).append(
" WHERE COS_MODEL = 'cos4' " ).append(
" AND DIRECTION = ? " ).append(
/*
* add my Flag case here
* */
" and TRAFFIC_PROFILE_ID IN " ).append(
" ( " ).append(
" select distinct (C1.Traffic_PROFILE_ID) from Cos_Class_Allocation C1, " ).append(
" Cos_Class_Allocation C2, Cos_Class_Allocation C3, Cos_Class_Allocation C4 " ).append(
" where c1.class_name = 'COS1' AND c1.CLASS_ALLOCATION = ? " ).append(
" and c2.class_name = 'COS2' AND c2.CLASS_ALLOCATION = ? " ).append(
" and c3.class_name = 'COS3' AND c3.CLASS_ALLOCATION = ? " ).append(
" and c4.class_name = 'COS4' AND c4.CLASS_ALLOCATION = ? " ).append(
" and C1.TRAFFIC_PROFILE_ID = c2.TRAFFIC_PROFILE_ID " ).append(
" and C1.TRAFFIC_PROFILE_ID = c3.TRAFFIC_PROFILE_ID " ).append(
" and C1.TRAFFIC_PROFILE_ID = c4.TRAFFIC_PROFILE_ID); " );
【问题讨论】:
-
小心,ce==pe 不对,是字符串,必须和.equals比较
-
您想在代码中添加 ce、pe 还是两者都添加?
-
为 ifs 创建一个自己的方法并在那里返回 String。其他选项是使用更动态的标准。
-
@melli-182 是附加取决于 if 语句条件