MySQL查询结果映射到map,如何将数据库查询映射到[Java]中的对象?

I have a query that joins 5 tables.

Then I fill my hand-made object with the column values that I need.

What are solutions here that are wide-common to solve that problem using specific tools ? are there such tools?

I'm only beginning to learn Hibernate, so my question would be: is Hibernate the right decision for this problem?

Hibernate maps a table to a class. So, there's no difference if I would have 5 classes instead of 5 tables. It would still be difficult to join the query result into a class

Could hibernate be used to map THE QUERY into the structure (class) I would define beforehand as we do with table mapping? Or even better, can it map the query result into the meaningful fields [auto-create the class with fields] as it does with reverse-engineering?

I've been thinking about views but.. create a new view everytime we need a complex query.. too verbose.

As S.Lott asked, here is a simple version of a question:

General problem:

select A.field_a, B.field_b, C.field_c

from table_a A inner join table_b B inner join table_c C

where ...

every table contains 100 fields

query returns 3 fields, but every field belongs to the unique table

How do I solve that problem in an OO style?

Design a new object with properties corresponding to the returning values of the query.

I want to know if it is the right [and the only one possible] decision and are there any common solutions.

See also my comments.

解决方案

There are a few options:

Create a single table mapping using elements for the related tables. A join in that way will allow other tables to contribute properties to your class.

Use a database view as previously suggested.

Use a Hibernate mapping view - instead of . It's essentially creating a view on the Hibernate side so the database doesn't have to change. The generated sql will end up looking like "select * from (select A.field_a, B.field_b from A, B, ...)

". I know that works in Oracle, DB2, and MySQL

All that is fine for selecting; if you need to do insert/update, you'll probably need to rethink your data model or your object model.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值