[英]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.我在使用 sql 本机查询通过 JPA 选择一些数据时遇到了一些麻烦。 That's because I have 3 columns with the same name, "descricao".那是因为我有 3 个同名的列,“descricao”。
When I execute the select operation through the createNativeQuery method of the EntityManager interface the first column value found overrides the others.当我通过EntityManager接口的createNativeQuery方法执行 select 操作时,找到的第一列值会覆盖其他列值。
(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) (例如,给定记录的第一列描述的值是“foo”,第二个“bar”和第三个“foobar”,当我在对象数组中得到这个结果时(因为我没有 ORM 映射实体),任何地方都应该用给定的列的第二个和第三个值填充 descicao 用第一个的值填充)
I'm quite sure that's because I've used JPA once selecting directly on the database return everything properly.我很确定那是因为我使用了 JPA 一旦直接在数据库上选择返回一切正确。
Environment:环境:
MySQL5; MySQL5; EJB 3.0; EJB 3.0; JPA 1.0; JPA 1.0; JBoss 5.0.0GA; JBoss 5.0.0GA; JDK 1.6; JDK 1.6;
SQL query: 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
..."
Scalar Column Mappings in Entity Bean:实体 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..." “选择 p.id, p.datapedido, b.descricao 作为 B_DESCRICAO, prd.descricao 作为 PRD_DESCRICAO, s.nome, usuario.email, cc.chave_cupom, prd.nome, ca.descricao 作为 CA_DESCRICAO, i.produto_id, i. valoritem,hc.valor_utilizado,tp.datapagamento..."
Creating native query by specifying resultSetMapping & query.通过指定 resultSetMapping 和查询来创建本机查询。
entityManager.createNativeQuery(queryString, "DescricaoColumnAlias");
I think you should use SqlResultSetMapping to specify the how the columns map to the properties of the entities.我认为您应该使用SqlResultSetMapping来指定列 map 到实体属性的方式。
You might find this wiki page of Eclipselink project (JPA reference implementation) useful: http://en.wikibooks.org/wiki/Java_Persistence/Querying#Result_Set_Mapping您可能会发现 Eclipselink 项目(JPA 参考实现)的这个 wiki 页面很有用: 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?我不使用 JPA,所以如果不符合要求,请忽略,但如果实体未映射,那么为什么不能在查询中为受影响的字段设置别名并相应地访问结果集?
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.