从 hbm 文件生成 Hibernate 复合键对象

发布于 2024-09-10 11:33:52 字数 615 浏览 5 评论 0原文

(括号内的咆哮:)我是一个 Hibernate 初学者,对我可以在 15 秒内用 SQL 编写的简单连接的开销感到有点沮丧。 (结束咆哮)

情况是这样的:我有两个表,它们都包含相同的复合主键 - 让我们将两个表中的列称为“ID”和“版本”。我需要两个表中有关特定 ID/版本的数据,因此

select a.xxx, b.yyy 
from tableA a, tableB b
where a.ID = b.ID
    and a.Version = b.Version
    and .....

表设计很糟糕,但不在我手中。

在 Hibernate 中,我的在线研究建议创建一个单独的复合键类以供表共享,其中 A 对象包含 B 对象,并且在 A 的 hbm 文件中它们之间存在一对一映射。

在我的项目中,我的所有 Hibernate 数据对象都是使用 hbm 文件生成的,并且位于同一位置。所以我想知道是否可以使用 hbm 文件创建这个复合键对象并将其与其兄弟一起存储。问题是,它没有映射到任何特定的(或者,您可以说它映射到 2 个表)。我正在尝试确定是否可以使用 hbm 文件创建此复合键类,以及是否建议这样做。我是否以错误的方式处理这个问题?

提前致谢。

(Parenthetical rant:) I'm a Hibernate beginner getting slightly frustrated by the overhead of a simple join that I could write in 15 seconds in SQL. (End rant)

The situation is thus: I have 2 tables that both contain the same composite primary key - let's call the columns in both tables 'ID' and 'Version'. I need data about a particular ID/Version from both tables, so

select a.xxx, b.yyy 
from tableA a, tableB b
where a.ID = b.ID
    and a.Version = b.Version
    and .....

Bad table design, but not in my hands.

In Hibernate, my online research suggests creating a separate composite key class to be shared by the tables, with an A object containing a B object, and a one-to-one mapping between them in A's hbm file.

In my project, all my Hibernate data objects are generated with hbm files and located in the same place. So I'd like to know if I can create this composite key object also using an hbm file and store it with its brethren. The issue is, it doesn't map to any table in particular (or, you could say it maps to 2 tables). I'm trying to determine if it's possible to create this composite key class with an hbm file, and also if it's advisable or not to do so. Am I going about this the wrong way?

Thanks in advance.

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

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

发布评论

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

评论(1

时光清浅 2024-09-17 11:33:52

我认为您误解了这个关键对象。您应该编写一个java类来表示这个键,在本例中由一个id和一个版本组成。然后使用此类作为复合主键类型。您在数据库中看不到有关此类的任何信息。它仅由您和 hibernate 使用来识别实例。

一对一关系用于同步主键。一个对象应该引用另一个对象,并从中获取自己的主键。

文档:

大多数从 SQL 切换到 Hibernate 的人都会感到沮丧,因为他们尝试像使用 SQL 一样使用 hibernate。然后他们不会从中受益,但会获得额外的开销。

所以这是我的简短建议:
你不应该再用表格来思考。除非您正在编写映射文件,否则就忘记表。只考虑类、对象和面向对象的结构。不要对您想要“在数据库中执行的操作”的所有内容执行查询。在大多数事务中,您在开始时执行单个查询,然后使用延迟加载(您的代码无法识别)沿属性导航。实现实际逻辑的代码不再了解有关数据库的任何信息。然后使用 hibernate 工作就开始变得有趣了。

I think you misunderstood this key object. You should write a java class, which represents this key, consisting of a id and a version in this case. Then you use this class as composite primary key type. You can't seen anything about this class in the database. It is just used by you and by hibernate, to identify an instance.

The one-to-one relation is used to synchronize primary keys. One object should reference the other, and will get its own primary key from that.

Documentation:

Most people who switch from SQL to Hibernate are frustrated, because they try to work with hibernate like with SQL. Then they don't benefit from it but get the additional overhead.

So here my short advise:
You should not think in tables anymore. Unless you are writing mapping files, just forget tables. Only think in classes, objects and object oriented structures. Don't perform queries for everything you want to "do in the database". In most transactions, you perform a single query at the beginning, then you navigate along properties, using lazy-loading (which your code doesn't recognize). The code which implements the actual logic does not know anything about a database anymore. Then it starts to be fun to work with hibernate.

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