English 中文(简体)
JPQL query SELECT optional + generic DAO select
原标题:

I have followed a working JPA example to retrieve Category objects as such:

return (ArrayList<Category>) getEntityManager().createQuery("from Category").getResultList();

The query is very shorthand - and I can t find the rules for what is optional and what isn t in any of the guides. Is this brevity acceptable?

Secondly, I want to now implement this in a generic DAO, something such as:

public interface DAO<E, K>
{
    List<E> getAll();
}

How can I rewrite the first query to work for all types as I can t hardcode the "from Category"..?

最佳回答
  1. Yes, the brevity is acceptable. Although I prefer the full syntax because it is more "appealing" to others who have more SQL experience.

  2. You have to add a Class<E> parameter to your DAO:

    public List<E> getAll(Class<E> entityClass) {
         Query query = enittyManager.createQuery("from " + entityClass.getName());
         query.getResultList();
    }
    
问题回答

You actually don t have to use a method parameter, but can use Reflection.

Example Code using Reflection

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class<T> DAO {         
    protected Class<T> clazz;

    public DAO()
    {
      Type genericSuperclass = getClass().getGenericSuperclass();
      // Allow this class to be safely instantiated with or without a parameterized type
      if (genericSuperclass instanceof ParameterizedType)
        clazz = (Class<T>) ((ParameterizedType) genericSuperclass).getActualTypeArguments()[0];
    }
}




相关问题
Recommended way to develop using Jetty and Eclipse

I am currently developing a J2EE application and I would like to use Jetty. I would like to have iot integrated with Eclipse, so I could debug the appliaction. I ve tried out couple of plugins (...

Call function periodically in Java

we need run one function periodically in Java web application . How to call function of some class periodically ? Is there any way that call function when some event occured like high load in server ...

Why make an EJB rather than a Web Service?

I would have thought that there is a lot of information out there on this, but I haven t found anything that really answers my question. What are the advantages of making an EJB rather than a web ...

Where should I put this configuration setting?

I m designing a fairly small web application which will run on a Sun application server (v9.1). It only has a few pages, no database of its own, and will retrieve/update data via web services. There s ...

JNDI Names -- Is Prefix "jdbc/" needed?

What s up with JNDI names? I m trying to get a javax.sql.DataSource using the new annotations feature of Java 5. It s not working for me, so I want to ask... I have a in my web.xml, inside of it is ...

hibernate interceptors : afterTransactionCompletion

I wrote a Hibernate interceptor : public class MyInterceptor extends EmptyInterceptor { private boolean isCanal=false; public boolean onSave(Object entity, Serializable arg1, Object[] arg2, String[]...

热门标签