问题描述
我试图获取所有管理员的名字和姓氏列表。
在我的下面的代码中有两个警告。我已经尝试在网上搜索很多。
1)查询是一种原始类型。
2)来自类型Query的方法list()已被弃用。
public List< Object> loadAllAdmins(){
List< Object> allAdmins = new ArrayList< Object>();
尝试{
HibernateUtil.beginTransaction();
Query q = currentSession.createQuery(从AdminBean admin选择admin admin,firstname,admin.lastName);
allAdmins = q.list();
HibernateUtil.commitTransaction();
} catch(HibernateException ex){
System.out.println(List< AdminBean> loadAllPersons:HibernateException);
}
返回allAdmins;
}
但是我在网上看到了类似这样的示例代码。我应该如何解决这两个问题?
更新
建议使用Criteria。它还表示list()方法不适合Criteria ...似乎很多方法都不适用于Query和Criteria,包括uniqueResult()和其他方法...任何建议我应该如何替换它们?
public List< Admin> getAdmins(){
List< Admin> AdminList = new ArrayList< Admin>();
会话会话= factory.openSession();
for(Object oneObject:session.createQuery(FROM Admin)。getResultList()){
AdminList.add((Admin)oneObject);
}
session.close();
返回AdminList;
}
警告来自Type Inference。
我有类似的问题。然而,我找到了一个没有SuppressWarnings的解决方案。
最近,我发现了一种更简单的方法来编码相同的东西
public List< Admin> getAdmins(){
Session session = factory.openSession();
TypedQuery< Admin> query = session.createQuery(FROM Admin);
列表<管理> result = query.getResultList();
session.close();
返回结果;
}
希望它有帮助。
I'm new to Hibernate.
I'm trying to get a list of first name and last name of all administrators.
There are two warnings in my following code. I already tried to search a lot online.
1) Query is a raw type. References to generic type Query should be parameterized.
2) The method list() from the type Query is deprecated.
public List<Object> loadAllAdmins() {
List<Object> allAdmins = new ArrayList<Object>();
try {
HibernateUtil.beginTransaction();
Query q = currentSession.createQuery("SELECT admin.firstName, admin.lastName from AdminBean admin");
allAdmins= q.list();
HibernateUtil.commitTransaction();
} catch (HibernateException ex) {
System.out.println("List<AdminBean> loadAllPersons: HibernateException");
}
return allAdmins;
}
But I see sample code like this all over the web. How should I solve these two problems?
Update
I just tried to use Criteria as suggested. It also says the list() method is deprecate for Criteria... It seems that a lot of methods are deprecate for both Query and Criteria, including uniqueResult() and others... Any suggestion how I should replace them?
public List<Admin> getAdmins() {
List<Admin> AdminList = new ArrayList<Admin>();
Session session = factory.openSession();
for (Object oneObject : session.createQuery("FROM Admin").getResultList()) {
AdminList.add((Admin)oneObject);
}
session.close();
return AdminList;
}
The warnings came from "Type Inference".
I had the similar problem. However, I found a solution without "SuppressWarnings".
Recently, I found out a shorter way to code the same things without type inference.
public List<Admin> getAdmins() {
Session session = factory.openSession();
TypedQuery<Admin> query = session.createQuery("FROM Admin");
List<Admin> result = query.getResultList();
session.close();
return result;
}
Hope it helps.
这篇关于Hibernate 5.2版本 - >很多查询方法不推荐使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!