本文介绍了使用Hibernate中的原生SQL查询将结果集导入DTO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类似于下面的查询
select f.id,s.name,ss.name
from第一个f
在f.id = s.id中加入第二个s
在f.sId = ss.id中加入第二个ss
如果我可以使用HQL,我会使用直接用结果集填充DTO。
但是,由于hibernate不允许在没有关联的情况下进行左连接,我必须使用Native SQL Query。
目前我正在循环以JDBC风格设置结果集并填充DTO对象。
有没有更简单的方法来实现它?
解决方案
你也许可以使用结果转换器。引用:
$ b
引用
- Hibernate参考指南
- Hibernate的博客
I have a query like below
select f.id, s.name, ss.name
from first f
left join second s on f.id = s.id
left join second ss on f.sId = ss.id
If I could use HQL, I would have used HQL constructor syntax to directly populate DTO with the result set.But, since hibernate doesn't allow left join without having an association in place I have to use the Native SQL Query.
Currently I am looping through the result set in JDBC style and populating DTO objects.Is there any simpler way to achieve it?
解决方案
You could maybe use a result transformer. Quoting Hibernate 3.2: Transformers for HQL and SQL:
References
- Hibernate Reference Guide
- Hibernate's Blog
这篇关于使用Hibernate中的原生SQL查询将结果集导入DTO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!