Java 使用if语句进行Hibernate查询

Java 使用if语句进行Hibernate查询,java,sql,hibernate,Java,Sql,Hibernate,我正在尝试编写一个hibernate查询,我正在使用一个StringBuilder,为ce、pe和两者分别设置3组字符串。如何将其添加到hibernate查询中?我想把它添加到我在“AddFlaghere”中添加注释的地方。我的标志类型是枚举“Y”或“N” 使用另一个变量存储要附加的代码部分: StringBuffer queryByCos4Allocation = new StringBuffer(); Flag ceIngress = null; Flag ceEgress = null;

我正在尝试编写一个hibernate查询,我正在使用一个StringBuilder,为ce、pe和两者分别设置3组字符串。如何将其添加到hibernate查询中?我想把它添加到我在“AddFlaghere”中添加注释的地方。我的标志类型是枚举“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;
String toAppend = "";

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') ";
    toAppend=ce;
}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') " ;         toAppend=pe;
}if (ce!=null && ce.equals(pe)){
    both =    " AND (CE_INGRESS_FLAG = 'Y' OR CE_EGRESS_FLAG = 'Y')  "
            + " AND (PE_INGRESS_FLAG = 'Y' OR PE_EGRESS_FLAG = 'Y') " ;         toAppend=both;
}
queryByCos4Allocation.append("  SELECT * FROM TRAFFIC_PROFILE " ).append( 
            "   WHERE COS_MODEL = 'cos4'  " ).append(
            "   AND DIRECTION = ?  " ).append(
                    toAppend +
            "   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不正确,它们是字符串,必须与进行比较。是否要在代码中附加ce、pe或两者?为ifs创建一个自己的方法并在那里返回字符串。其他选项是使用更动态的条件。@melli-182 yes append取决于if语句条件
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;
String toAppend = "";

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') ";
    toAppend=ce;
}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') " ;         toAppend=pe;
}if (ce!=null && ce.equals(pe)){
    both =    " AND (CE_INGRESS_FLAG = 'Y' OR CE_EGRESS_FLAG = 'Y')  "
            + " AND (PE_INGRESS_FLAG = 'Y' OR PE_EGRESS_FLAG = 'Y') " ;         toAppend=both;
}
queryByCos4Allocation.append("  SELECT * FROM TRAFFIC_PROFILE " ).append( 
            "   WHERE COS_MODEL = 'cos4'  " ).append(
            "   AND DIRECTION = ?  " ).append(
                    toAppend +
            "   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); " );