提取对象内部的值 [英] extracting the values inside an object

查看:150
本文介绍了提取对象内部的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的代码

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆