0

I am trying to write a hibernate query and I am using a StringBuilder and set 3 separate set of strings for ce, pe, and both. How can I add it to my hibernate query? I want to add it where i added a comment to "add flag here". My Flag type are enums "Y" or "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); " );
4
  • 2
    be carefull, ce==pe is not right, they are Strings and must be compared with .equals Commented Aug 28, 2015 at 17:56
  • do you want to append ce, pe or both to your code? Commented Aug 28, 2015 at 17:57
  • Make a own method for ifs and return String there. Otheroption is use of Criteria more dynamic. Commented Aug 28, 2015 at 18:02
  • @melli-182 yes append depending on the if statement condition Commented Aug 28, 2015 at 18:38

1 Answer 1

2

Use another variable to store what part of the code you want to append:

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); " );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.