Java Persistence:转换为Query.getResultList()的结果的东西? [英] Java Persistence: Cast to something the result of Query.getResultList()?
问题描述
嘿,大家,我是新的持久性/休眠,我需要你的帮助。
Hey everyone, I'm new to persistence / hibernate and I need your help.
这里的情况。我有一个表,其中包含一些东西。让我们称他们为人。
我想从数据库中获取该表中的所有条目。
Here's the situation. I have a table that contains some stuff. Let's call them Persons. I'd like to get all the entries from the database that are in that table.
我有一个Person类,它是一个简单的POJO,
I have a Person class that is a simple POJO with a property for each column in the table (name, age,..)
这里是我有的:
Query lQuery = myEntityManager.createQuery("from Person")
List<Person> personList = lQuery.getResultList();
$ 到
列表< Person>
我认为只需将代码更改为
I thought that simply changing the code to
Query lQuery = myEntityManager.createQuery("from Person")
List<Person> personList = (List<Person>)lQuery.getResultList();
会工作..但不会。
有办法吗?持久性是否允许我设置查询的返回类型? (通过泛型可能?)
Is there a way to do this ? Does persistence allow me to set the return type of the query ? (Through generics maybe ? )
推荐答案
作为JPA的新来者把这个作为最终答案,通过此问题:为什么在JPA EntityManager查询中
As a newcomer to JPA was taking this as the definitive answer but then I found a better one via this question: Why in JPA EntityManager queries throw NoResultException but find does not?
它与使用TypedQuery而不是Query一样简单eg:
Its as simple as as using TypedQuery instead of Query e.g.:
TypedQuery<Person> lQuery = myEntityManager.createQuery("from Person", Person.class);
List<Person> personList = lQuery.getResultList();
这篇关于Java Persistence:转换为Query.getResultList()的结果的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!