如何添加泛型类型?

时间:2016-01-11 18:17:20

标签: java hibernate

我在hibernate stuts2 java中使用了以下代码。

  String hql = "from PostDetails";
  Query postDetails = session.createQuery(hql); // Got warning from here
  List<PostDetails> result = postDetails.list();

警告是:Type safety: The expression of type List needs unchecked conversion to conform to List<PostDetails>

如果我使用此注释@SuppressWarnings(“未选中”)则会解决警告。但我不想任何注释。

如何修复此警告?

1 个答案:

答案 0 :(得分:0)

我不相信你可以。甚至Hibernate的documentation and examples也使用原始类型def self.import(file) CSV.foreach(file.path, headers: true) do |row| user = find_by_id(row["id"]) || new user.attributes = row.to_hash.slice(*accessible_attributes) user.save!(validate: false) end end 而不是类型的List。但是, 非常不可能 您将遇到该列表的错误; Hibernate不会返回在该上下文中无效的列表。

较新的标准版JPA API具有typed queries,可以避免这些警告,并允许您返回正确键入的列表。

这意味着您可以执行此操作以获取所需的列表:

List<Student> = em.createQuery("select s from School s", Student.class).getResultList();