If the Pizza entity has a "name" field, and you generate the "findPizzasByNameLike" method, it looks like this:
The above code searches for Pizzas with the given string in their name case-sensitively. So if the user enters "meat" as the search string, they will find "Ratmeat Surprise" but not "Meat Lovers". This is not what most users would expect. The search function in commonly used apps like Google, GMail, and MS Office is case-insensitive by default.Code:public static Query Pizza.findPizzasByNameLike(String name) { if (name == null || name.length() == 0) throw new IllegalArgumentException("The name argument is required"); name = name.replace('*', '%'); if (name.charAt(0) != '%') { name = "%" + name; } if (name.charAt(name.length() -1) != '%') { name = name + "%"; } EntityManager em = Pizza.entityManager(); Query q = em.createQuery("SELECT Pizza FROM Pizza AS pizza WHERE pizza.name LIKE :name"); // * q.setParameter("name", name); // * return q; }
Therefore to match the most common use cases, the finders that Roo generates should be case-insensitive (or at least accept a boolean parameter that indicates whether to search case sensitively). The workaround is to copy-and-paste the above finder into your Pizza.java file, fix the compile errors, and change the starred lines to read:
If you wanted to be really correct, you should also pass in the user's Locale so that the lower-casing can be done in a locale-specific way.Code:Query q = em.createQuery("SELECT Pizza FROM Pizza AS pizza WHERE LOWER(pizza.name) LIKE :name"); q.setParameter("name", name.toLowerCase());
Should I log this as an improvement?


Reply With Quote
