I have 'improved' the BuildCriteria to handle columns of type Integer. I
had to use addSql because addLike causes a class cast exception if the
field type is of Integer.

public Criteria buildCriteria (AnObject anExample) throws Exception{
    Criteria criteria = new Criteria();
    ClassDescriptor cld = MetadataManager.getInstance ().getRepository
().getDescriptorFor (anExample.getClass ());
    FieldDescriptor [] fds = cld.getFieldDescriptions (); PersistentField
    f;

    for (int i = 0; i < fds.length; i++){
        f = fds [i].getPersistentField ();
        String colValue = f.get (anExample).toString(); String colName =
        f.getName ();

        if (!colValue.equals("") && !colValue.equals(" ")) {
            if ((f.getType()) == Class.forName("java.lang.String")){
                criteria.addLike (colName, colValue);
            } else if ((f.getType()) ==
            } Class.forName("java.lang.Integer")) {
                criteria.addSql(colName + " LIKE " + "'" + colValue +"'");
            }
            }
        }
            
    return criteria;
}
}
(It may not be the most performant bit of code but I have only been
developing with java for 2 months now)

I am now going to try and refine it so that it detects if the user has
input a primary key (i.e. just do select * from table where pkey = value)
and then I guess I will have to look at dates and < / >, oh good!

thanks again for pointing me in the right direction


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to