Menu

Possible bug in generated loadUsingTemplate()

Help
rjemmons
2008-04-14
2013-04-25
  • rjemmons

    rjemmons - 2008-04-14

    I think I may have found a bug in the code generated by Sql2java.  I reproduced the code generated for my application below.  Although I send it a good bean, it generates a DAOException every time it is called.

    At the beginning of the try{} block there is an if{} else{} block.  The else portion only contains 2 test statements which are commented out.  Now the "c = this.getConnection();" statement only executes if the if{} condition fails.  This apparently causes the DAOException.

    I think you need to comment out the else, or add a semi-colon.

    Can you confirm that this is a bug, and provide a fix?

        public UserReportBean[] loadUsingTemplate(UserReportBean bean, int startRow, int numRows, int searchType) throws DAOException
        {
            // System.out.println("loadUsingTemplate startRow:" + startRow + ", numRows:" + numRows + ", searchType:" + searchType);
            Connection c = null;
            PreparedStatement ps = null;
            StringBuffer sql = new StringBuffer("SELECT " + ALL_FIELDS + " FROM user_report ");
            StringBuffer sqlWhere = new StringBuffer("");

            try
            {
                if (this.fillWhere(sqlWhere, bean, searchType) > 0)
                    sql.append(" WHERE ").append(sqlWhere);
                else
                    // System.out.println("The bean to look is not initialized... loading all");
                    // System.out.println("loadUsingTemplate: " + sql.toString());

                c = this.getConnection();
                int scrollType = ResultSet.TYPE_SCROLL_INSENSITIVE;
                if (startRow != 1)
                    scrollType = ResultSet.TYPE_SCROLL_SENSITIVE;
                ps = c.prepareStatement(sql.toString(),
                                        scrollType,
                                        ResultSet.CONCUR_READ_ONLY);
                this.fillPreparedStatement(ps, bean, searchType);

                ps.executeQuery();
                return this.loadByPreparedStatement(ps, null, startRow, numRows);
            }
            catch(SQLException e)
            {
                throw new ObjectRetrievalException(e);
            }
            finally
            {
                this.getManager().close(ps);
                this.freeConnection(c);
                sql = null;
                sqlWhere = null;
            }
        }

     
    • Alain Fagot Béarez

      This bug was present in the 2.6.5 version. It has been corrected in the 2.6.6 release.

      The correct code should read as follow:

                  if (this.fillWhere(sqlWhere, bean, searchType) > 0) {
                      sql.append(" WHERE ").append(sqlWhere);
                  } else {
                     // System.out.println("The bean to look is not initialized... loading all");
                  }
                  // System.out.println("loadUsingTemplate: " + sql.toString());

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.