10 Replies Latest reply on Mar 9, 2012 12:51 PM by cyberanto

    How to pass parameters to List objects?

    jinghuiyin

      I have a DocumentDbList class for my datatable.  In the class I have:

       

      @Override

      public String getEjbql() {

          return "select documentDb from DocumentDb documentDb";

      }

       

      This gives me the data of whole table.  However I need to be able to show only the data that has studentId equal to certain number.  How do I pass the parameter studentId in DocumentDbList class?

       

      Thanks,

      Jenny

        • 1. Re: How to pass parameters to List objects?
          gebuh

          Use restrictions:

           

          private static final String[] RESTRICTIONS = {
                      "lower(documentDb.field) like lower(concat(#{documentDbList.documentDb.field},'%'))",
                      "lower(documentDb.otherField) like lower(concat(#{documentDbList.documentDb.otherField},'%'))",};
          setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
          
          • 2. Re: How to pass parameters to List objects?
            jinghuiyin

            Yes I've seen this.  However i don't know how / where I can set the value for #{documentDbList.documentDb.field}?  Pass it in as a request parameter?  Or how?  Please help.

             

            Thanks!

            • 3. Re: How to pass parameters to List objects?
              gebuh

              What do you have so far?  Is this for a search page?

              • 4. Re: How to pass parameters to List objects?
                jinghuiyin

                It's not a search page.  I have studentId passed into this page as query string.  And on this page, I need to show datatable with only #{documentDbList.documentDb.studentId} equal to this passed in studentId.  How do I set it?

                 

                Thanks!

                • 5. Re: How to pass parameters to List objects?
                  jinghuiyin

                  Since I don't know how to set parameters for List objects, I'd like to use List<DocumentDb> as the data store for the datatable.  will that be viable?  Especially the sortable column header part?

                  • 6. Re: How to pass parameters to List objects?
                    gebuh

                    I need some context, how are you passing the studentId to the page?  Are you using pages.xml?  Post what you have so far.

                    • 7. Re: How to pass parameters to List objects?
                      jinghuiyin

                      I got it working by putting it in the where clause:

                       

                      @Override

                      public String getEjbql() {

                          return "select documentDb from DocumentDb documentDb where documentDb.studentProfile.studentId=#{studentProfileHome.instance.studentId}";

                      }

                       

                      Thanks Beth for your help!

                      • 8. Re: How to pass parameters to List objects?
                        kragoth

                        Qingxin Yi wrote:

                         

                        I got it working by putting it in the where clause:

                         

                        @Override

                        public String getEjbql() {

                            return "select documentDb from DocumentDb documentDb where documentDb.studentProfile.studentId=#{studentProfileHome.instance.studentId}";

                        }

                         

                        Thanks Beth for your help!

                        Just remember that with this solution you are opening yourself up for SQL injection attacks.

                        • 9. Re: How to pass parameters to List objects?
                          jinghuiyin

                          So can you suggest a better solution to this?

                           

                          Thanks!

                          • 10. Re: How to pass parameters to List objects?
                            cyberanto

                            As Beth pointed out, this should be:

                             

                            private static final String EJBQL = "select documentDb from DocumentDb documentDb";

                             

                                      private static final String[] RESTRICTIONS = {

                                                "documentDb.studentProfile.studentId=#{studentProfileHome.instance.studentId}",};

                             

                              // constructor - whatever the name of your class is

                                      public DocumentDBQuery() {

                                                     setEjbql(EJBQL);

                                                     setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));

                                                     setMaxResults(25);

                                      }

                             

                            depending on requirements & re-usability goals, you may also consider using a Criteria Seam Component (set up in component.xml) of StudentProfile class  to make your queries more flexible. Look at "Seam in Action" 10.4. Smarter Queries with the Query Component, and 10.4.5 Placing restrictions on the result set - Query by Example