在休眠 createSQlquery 结果中使用 sql 列名 [英] Using sql column names in hibernate createSQlquery result

查看:23
本文介绍了在休眠 createSQlquery 结果中使用 sql 列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个带有复合主键的 sql 视图,我想查询它们,并且由于 Hibernate 使使用复合键变得很痛苦,我正在使用 createSQLQuery.问题是这个方法只能返回一个List,我需要通过它们的索引来引用这些列.

I have a couple of sql views with composite primary keys that I want to query, and since Hibernate makes it a pain to work with composite keyes, I'm using createSQLQuery. The problem is that this method can only return a List, and I need to refer to the colums by their index.

我有没有可能做一些类似 jdbc 的事情,并通过它们的 sql 名称而不是它们的索引来引用列?

Any chance I could do something like jdbc and refer to the columns by their sql name instead of their index?

推荐答案

Query query=session.createSQLQuery("your query");
query.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
List<Map<String,Object>> aliasToValueMapList=query.list();

从代码中可以看出,该列表包含代表每一行的 Map 对象.每个 Map 对象都将列名作为键和值作为值.

As you can figure out from code, the list contains Map objects representing each row. Each Map object will have column name as key and value as value.

注意:这适用于 SQLQuery,如果您在 hql 查询上使用 AliasToEntityMapResultTransformer 而不指定别名,您将获得索引值作为键.

Note: This work for SQLQuery, if your using AliasToEntityMapResultTransformer on hql query without specifying aliases you will get index value as key.

如果您再次将 aliasToValueMapList 转换为您的 POJO 列表,我建议您创建自己的ResultTransformer 并从'transformTuple' 方法.

If you are again transforming aliasToValueMapList to your POJO list, I advice you to create your own ResultTransformer and return your custom object from 'transformTuple' method.

这篇关于在休眠 createSQlquery 结果中使用 sql 列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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