JPQL 查询 SELECT 可选 +通用 DAO 选择

发布于 2024-08-21 16:06:29 字数 388 浏览 4 评论 0原文

我遵循一个有效的 JPA 示例来检索类别对象,如下所示:

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

查询非常简写 - 我找不到关于什么是可选的以及什么不在任何指南中的规则。这种简洁可以接受吗?

其次,我现在想在通用 DAO 中实现这一点,例如:

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

如何重写第一个查询以适用于所有类型,因为我无法对“来自类别”进行硬编码..?

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"..?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝海 2024-08-28 16:06:29
  1. 是的,简洁是可以接受的。虽然我更喜欢完整的语法,因为它对其他有更多 SQL 经验的人来说更“有吸引力”。

  2. 您必须向 DAO 添加一个 Class 参数:

    公共列表; getAll(ClassentityClass) {
         查询 query = enityManager.createQuery("from " +entityClass.getName());
         查询.getResultList();
    }
    
  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();
    }
    
梦太阳 2024-08-28 16:06:29

您实际上不必使用方法参数,但可以使用反射。

使用反射的示例代码

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];
    }
}

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];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文