开发者

How to select multiple columns with the same name using JPA native query?

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".

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

(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)

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

Environment:

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

SQL query:

"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
            ..."


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.

"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..."

Creating native query by specifying resultSetMapping & query.

entityManager.createNativeQuery(queryString, "DescricaoColumnAlias");


I think you should use SqlResultSetMapping to specify the how the columns map to the properties of the entities.

You might find this wiki page of Eclipselink project (JPA reference implementation) useful: http://en.wikibooks.org/wiki/Java_Persistence/Querying#Result_Set_Mapping


I don't use JPA, so ignore if off the mark, but if the entities are not mapped, then why can you not alias the affected fields in your query and access the result set accordingly?

select b.descricao AS d1, prd.descricao as d2, ca.descricao as d3...


@Transactional(readOnly = true)
@SuppressWarnings("unchecked")
@Column(name = "status")

public List<Student> findStudentByStatus(String status) {
    System.out
            .println("call findStudentMethd******************with this pattern"
                    + status
                    + "*********************************************");

    return em.createQuery(
            "select attendence from Attendence attendence where attendence.status like '"
                    + p
                    + A 
                    + L 
                    + "'")

    .getResultList();

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜