如何使用JPA本机查询选择具有相同名称的多个列? [英] How to select multiple columns with the same name using JPA native query?

查看:138
本文介绍了如何使用JPA本机查询选择具有相同名称的多个列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用JPA的sql本地查询选择一些数据时遇到了一些麻烦。那是因为我有3个同名的列,descricao。

I'm having some troubles while selecting some data using sql native query through JPA. That's because I have 3 columns with the same name, "descricao".

当我通过 createNativeQuery 方法执行select操作时strong> EntityManager 接口找到的第一列值会覆盖其他列值。

When I execute the select operation through the createNativeQuery method of the EntityManager interface the first column value found overrides the others.

(例如,当我得到这个时,给定记录的第一列descricao的值是foo,第二个bar和第三个foobar的值导致一个对象数组(因为我没有ORM映射实体),在任何地方应该填充给定的第二个和第三个值descricao列填充第一个的值)

(eg. the value of the first column descricao of the given record is "foo", the second "bar" and the third "foobar", when I get this result in an array of objects (because I haven't ORM mapped the entities), wherever should be filled with the given second and third values of the column descricao are filled with the value of the first one)

我很确定这是因为我在使用JPA时直接在数据库上选择正确返回所有内容。

I'm quite sure that's because I've used JPA once selecting directly on the database return everything properly.

环境:

MySQL5;
EJB 3.0;
JPA 1.0;
JBoss 5.0.0GA;
JDK 1.6;

MySQL5; EJB 3.0; JPA 1.0; JBoss 5.0.0GA; JDK 1.6;

SQL查询:

"select p.id, p.datapedido, b.descricao, prd.descricao, s.nome,
            usuario.email, cc.chave_cupom, prd.nome,
             ca.descricao, i.produto_id, i.valoritem,
             hc.valor_utilizado, tp.datapagamento
            ..."


推荐答案

实体Bean中的标量列映射:

Scalar Column Mappings in Entity Bean:

@SqlResultSetMapping(
      name="DescricaoColumnAlias",
      columns={@ColumnResult(name="B_DESCRICAO"),
               @ColumnResult(name="CA_DESCRICAO"),
               @ColumnResult(name="PRD_DESCRICAO")}
)

现在为本机中的列使用别名列映射中指定的查询。

Now using alias for the columns in the native query as specified in column mappings.

选择p.id,p.datapedido,b.descricao作为B_DESCRICAO,prd.descricao作为PRD_DESCRICAO,s.nome,usuario。 email,cc.chave_cupom,prd.nome,ca.descric ao as CA_DESCRICAO,i.produto_id,i.valoritem,hc.valor_utilizado,tp.datapagamento ...

"select p.id, p.datapedido, b.descricao as B_DESCRICAO, prd.descricao as PRD_DESCRICAO, s.nome, usuario.email, cc.chave_cupom, prd.nome, ca.descricao as CA_DESCRICAO, i.produto_id, i.valoritem, hc.valor_utilizado, tp.datapagamento..."

通过指定resultSetMapping&创建本机查询。查询。

Creating native query by specifying resultSetMapping & query.

entityManager.createNativeQuery(queryString, "DescricaoColumnAlias");

这篇关于如何使用JPA本机查询选择具有相同名称的多个列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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