如何使用 Hibernate 将原始 sql 投影放入对象中?

发布于 2024-09-07 23:15:41 字数 349 浏览 3 评论 0原文

假设我有一个以以下投影开头的查询:

SELECT t.term as term, count(g.id) as amount

这是原始 sql,我在 Hibernate 会话对象上使用 createSqlQuery。我想做的是将这些投影放入具有“term”和“amount”属性的对象中。

使用 HQL,我们可以使用“select new ClassName(...)”,但这不适用于原始 SQL 查询。

我们该怎么做呢?我拿回了一堆 [LObject's...但我不知道如何处理它们。如果我能让 Hibernate 将它们放入某种非实体值对象中,那就太好了。

谢谢!

Let's say I have a query that begins with the following projections:

SELECT t.term as term, count(g.id) as amount

This is raw sql, and I am using createSqlQuery on the Hibernate session object. What I'd like to do is take these projections and put them in an object that has a "term" and "amount" properties.

With HQL, we can use "select new ClassName(...)", but this doesn't work with a raw SQL query.

How do we do it? I get back a bunch of [LObject's... and I have no idea what to do with them. If I can get Hibernate to put them into some kind of non-entity value object, that'd be great.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-09-14 23:15:41

如果我能让 Hibernate 将它们放入某种非实体值对象中,那就太好了。

您可以通过应用“结果转换器”来告诉 Hibernate 从本机 SQL 查询返回非托管值对象。来自参考文档:

16.1.5。返回非托管实体

可以申请
ResultTransformer 到本机 SQL
查询,允许它返回
非托管实体。

sess.createSQLQuery("从猫中选择姓名、出生日期")
        .setResultTransformer(Transformers.aliasToBean(CatDTO.class))

此查询指定:

  • SQL 查询字符串
  • 结果转换器

上面的查询将返回一个列表
已实例化的 CatDTO
并注入 NAME 的值和
BIRTHNAME 进入其对应的
属性或字段。

If I can get Hibernate to put them into some kind of non-entity value object, that'd be great.

You can tell Hibernate to return non-managed value objects from a native SQL query by applying a "result transformer". From the reference documentation:

16.1.5. Returning non-managed entities

It is possible to apply a
ResultTransformer to native SQL
queries, allowing it to return
non-managed entities.

sess.createSQLQuery("SELECT NAME, BIRTHDATE FROM CATS")
        .setResultTransformer(Transformers.aliasToBean(CatDTO.class))

This query specified:

  • the SQL query string
  • a result transformer

The above query will return a list of
CatDTO which has been instantiated
and injected the values of NAME and
BIRTHNAME into its corresponding
properties or fields.

梦萦几度 2024-09-14 23:15:41

检查 http://docs.jboss.org /hibernate/core/3.3/reference/en/html/querysql.html#d0e13696

sess.createSQLQuery("SELECT ID, NAME, BIRTHDATE, DOG_ID FROM CATS").addEntity(Cat.class)

假设您的对象具有 term 和 amount 属性,只有这 2 个对象属性,与您的原始查询类似SQL 应该返回这些对象的列表,然后您可以逐一迭代该列表。

Check http://docs.jboss.org/hibernate/core/3.3/reference/en/html/querysql.html#d0e13696

sess.createSQLQuery("SELECT ID, NAME, BIRTHDATE, DOG_ID FROM CATS").addEntity(Cat.class)

Assuming your object with the term and amount properties has only those 2 object attributes a similar query with your raw SQL should return a list of those objects which you can then iterate through one by one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文