db-torque-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Eric Emminger <e...@ericemminger.com>
Subject Re: Manual SQL loses sort order at populateObjects()
Date Wed, 02 Apr 2003 20:28:21 GMT
Donald

> The following code retrieves rows from MySql DB and is sorted properly based on println
below. After this method is called, a CustomerBean[] is populated with these results..see
code at end. The bean[] is being populated with unsorted objects(?). I suspect the populateObject
method is ignoring/undoing my sortorder, but all I can do to verify is println() getting the
memory address of the Customer objects. Any info on how to do this and maintain my sortorder
would be appreciated.
> 
> public class CustomerPeer
>     extends org.apache.torque.BaseCustomerPeer
> {
>  public static List getCustomersByPref(String state, String subject, int isPreferred
){
>  List results = null;
>   //Connection db = null;
>   String sql = "SELECT CUSTOMER.CUSTOMER_ID, CUSTOMER.CUSTOMER_NAME, CUSTOMER.RATING,
CUSTOMER.RATED_COUNT, CUSTOMER.PAID_AD, CUSTOMER.USE_URL, CUSTOMER.ADDED FROM CUSTOMER, CATEGORY_MINOR,
LOCATION WHERE CATEGORY_MINOR.CATEGORY_NAME LIKE '%" + subject + "%' AND LOCATION.FK_STATE='"
+ state + "' AND CUSTOMER.PAID_AD = " + isPreferred + " AND CATEGORY_MINOR.FKCUSTOMER_ID=CUSTOMER.CUSTOMER_ID
AND CUSTOMER.CUSTOMER_ID=LOCATION.FKCUSTOMER_ID ORDER BY CUSTOMER.CUSTOMER_NAME ASC";
>   try{
>    //db = Torque.getConnection(DATABASE_NAME);
>    //results = BasePeer.executeQuery( sql, db );
>    results = BasePeer.executeQuery( sql, "fishing" );

First, this looks like a fairly simple query. Why aren't you using 
Criteria and your Peer instead of a custom query?

> /*
> SORTED PROPERLY HERE
> */
>    System.out.println("Results List = " + results.toString());
>    return populateObjects(results);
>    //following results in java.lang.ClassCastException
>    //populateObjects(results); 
>    //System.out.println("Results List after popOb = " + results.toString());
>    //return results;
>   }
>   catch(Exception e){
>    System.out.println("Error in getCustomersByPref");
>    e.printStackTrace();
>   }
>     return null;
>  }
> }
> ****************************************************
> bean populated using the convention:
> //where marinas = results from query after populateObjects() has been called
> Map companies = new HashMap();

I'm not sure if this is the problem, but a HashMap does not guarantee 
order. Have you tried TreeMap?

> for( int j=0; j<marinas.size();j++ ){
>     CompanyBean current = new CompanyBean();
>     Customer pickme = (Customer)marinas.get(j);
>     ...
>     companies.put(Integer.toString(j), current );
> }
> CompanyBean[] result = new CompanyBean[companies.size()];
>    companies.values().toArray(result);
>    return (result);

Eric


Mime
View raw message