如何将数据库查询映射到[Java]中的对象? [英] How to map a database query into an Object [in Java]?

查看:494
本文介绍了如何将数据库查询映射到[Java]中的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个连接5个表的查询。
然后我用我需要的列值填充手工制作的对象。

这里有哪些解决方案是广泛使用的解决方案工具?有没有这样的工具?



我只是开始学习Hibernate,所以我的问题是:Hibernate是否是这个问题的正确决定?



Hibernate将表映射到类。所以,如果我有5个班级而不是5个班级,那么没有什么区别。将查询结果加入到类中仍然很难


可以使用hibernate将QUERY映射到结构(类)I会像我们使用表映射一样预先定义?或者甚至更好,它是否可以将查询结果映射到有意义的字段[自动创建带有字段的类],就像逆向工程一样?




我一直在考虑视图,但是..每次我们需要一个复杂的查询时创建一个新视图..太冗长了。



正如S.Lott所问,这是一个简单的问题版本:



常见问题:

 
从table_a中选择A.field_a,B.field_b,C.field_c
内部连接table_b B内连接table_c C
其中...

每个表包含100个字段
查询返回3个字段,但每个字段都属于唯一表格



如何以OO风格解决该问题?
使用与查询返回值相对应的属性设计一个新对象。



我想知道它是否是正确的[也是唯一可能]决定和是否有任何共同的解决方案。



另请参阅我的意见。

解决方案




  1. 使用< join> 元素的相关表。以这种方式进行连接将允许其他表为您的类提供属性。

  2. 使用之前建议的数据库视图。 使用Hibernate映射您可以使用< class name = ... select =来代替< class name = ... table = ... 从A,B,...> 中选择A.field_a,B.field_b ......。它本质上是在Hibernate方面创建一个视图,因此数据库不必更改。生成的sql最终看起来像 select * from(从A,B,...选择A.field_a,B.field_b ...)。我知道这在Oracle,DB2和MySQL中可行

所有选择都很好;如果您需要插入/更新,您可能需要重新考虑数据模型或对象模型。


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:

  1. Create a single table mapping using <join> elements for the related tables. A join in that way will allow other tables to contribute properties to your class.
  2. Use a database view as previously suggested.
  3. Use a Hibernate mapping view - instead of <class name=... table=... you can use <class name=... select="select A.field_a, B.field_b, ... from A, B, ...">. 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.

这篇关于如何将数据库查询映射到[Java]中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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