提取对象内部的值 [英] extracting the values inside an object
问题描述
我当前的代码
Object resultQuery;
EntityManagerFactory emf = Persistence.createEntityManagerFactory("TESTPU");
EntityManager em = emf.createEntityManager();
TypedQuery<Account> tp = em.createQuery("SELECT a FROM Account a WHERE a.email = :email AND a.pwd = :pwd AND a.role = 'admin'", Account.class);
tp.setParameter("email", this.username);
tp.setParameter("pwd", this.password);
resultQuery = tp.getSingleResult();
结果存储在resultQuery
对象中,但是我对如何提取对象内部的数据感到困惑.
the result is stored in the resultQuery
object, but i'm confused on how to extract the datas inside the object.
假设我要从resultQuery
对象中提取帐户表中的列name
let's say I want to extract the column name
inside the account table from the resultQuery
object
推荐答案
问题似乎出在resultQuery
的类型上.鉴于tp
的类型为TypedQuery<Account>
,我将*期待类似的内容:
It looks like the problem is the type of resultQuery
. Given that tp
is of type TypedQuery<Account>
I would *expect something like:
Account result = tp.getSingleResult();
...在这一点上,很容易从Account
获取名称.例如:
... at which point it's easy to get the name from the Account
, presumably. For example:
String name = result.getName();
在代码中是否有任何理由将resultQuery
键入为Object
而不是Account
?
Is there any reason for resultQuery
to be typed as Object
instead of Account
in your code?
我自己没有使用TypedQuery
,但是 getSingleResult()
的文档肯定表明这是正确的方法.
I haven't used TypedQuery
myself, but the documentation for getSingleResult()
certainly suggests this is the right approach.
这篇关于提取对象内部的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!