users@glassfish.java.net

Re: Metadata from the resultlist

From: Witold Szczerba <pljosh.mail_at_gmail.com>
Date: Tue, 8 May 2007 20:21:51 +0200

I am not sure if I understand correctly...

1. searchXXX(anytext)
2. search(class, key, value)
3. search(query)

What kind of metadata you would like to obtain?
ad.1) An example:
List<Person> searchPerson(anytext)
what does that "anyText" would stand for?

ad.2) I am not sure, but let me guess:
public <T> List<T> search(Class<T> clazz, String key, Object value) {
  @SuppressWarning("unchecked")
  List<T> result = em.createQuery(String.format(
            "SELECT e from %s e WHERE e.%s = ?1"
             ,clazz.toString(), key))
            .setParameter(1, value)
            .getResultList();
   return result;
}
Such an interface, in my opinion, does not make any sense, as it is
too restrict (for example one would like to use "LIKE" instead of
"="). After a while you would find it is useless. That is much simpler
to write in your code just what you want, like this:
List<Person> persons = em.createQuery(
    "SELECT p FROM Company c, IN(c.person) p WHERE c.name LIKE ?1")
    .setParameter(1, someTxtValue)
    .getResultList();
Now, you can find how many results are by calling persons.size().

ad.3)
List<Object[]> search(String query, Object... parameters) {
   Query q = em.createQuery(query);
   for (int i = 1;i<parameters.length;i++) q.setParameter(i,parameters[i]);
   @SuppressWarning("unchecked')
   List<Object[]> result = q.getResultList();
   return result;
}

This will work only, when your query would have more than one field in
select clause, for example: String query = "SELECT p.id,p.name,p.age
FROM Person p WHERE p=?1"
List<Object[]> persons = search(query, em.getReference(Person.class, 34));
your particular result: "persons" does have persons will have .size()
== 0 or 1, and types of values in each row can be checked, by asking:
 result.get(idx)[column].getClass()
The bad thing is that when your result is empty, you will not be able
to read column classes :/ I have no idea how tr get column types for
such a query, when it returns no rows.


2007/5/8, glassfish_at_javadesktop.org <glassfish_at_javadesktop.org>:
> I want to provide user an overloaded search method with following signatures:
>
> searchXXX(anytext) -- here we know that XXX is gonna be the return type so thats ok.
> search(class, key, value) -- here the return type has to be of type class but i could not achieve to set it as such.
> search(query) -- here the return type depends upon the query at runtime and i need metadata of the returning results.
> [Message sent by forum member 'ketancmaheshwari' (ketancmaheshwari)]
>
> http://forums.java.net/jive/thread.jspa?messageID=215961
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>