Hibernate<生成器>困惑

发布于 2024-10-05 22:03:05 字数 87 浏览 1 评论 0原文

我读过一些关于休眠的教程。我仍然感到困惑。如果发生关联,生成器是否会发生变化。有人可以根据他们的经验很好地解释如何使用它,因为这是我所有问题(例如违规)的根源。

I have read several tutorial concerning the for hibernate. I'm still getting confusion. Does the generator changes in case of associations. Can someone explain well according to their experience that worked how to use this because this is the source of all my problems such as violations.

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

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

发布评论

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

评论(1

绮烟 2024-10-12 22:03:05

如果我正确理解你的问题,你是在问,什么是生成器以及它们如何与关联一起使用?

第一个答案很简单:生成器定义了为对象分配 id 的策略。

第二个答案是:取决于你如何建立你们的关系。例如,查看基于主键的唯一一对一的文档

http://docs.jboss.org/hibernate/core/3.5/reference/en/html/mapping.html#mapping-declaration-onetoone

这里是生成器类一侧的设置为“foreign”,这是一种特殊情况,因为两个表共享相同的 id。

请注意另一个一对一的示例,在这种情况下,关系的一侧不会分配任何生成器。

如果您遇到持久性问题,我建议您执行以下 2 项:

  1. 专门为您的 DAO 编写集成测试。如果您没有 DAO,请创建它们以确保所有这些代码都位于一处。
  2. 仔细看看 hibernate 附带的示例代码和测试。这段代码很有启发性。

If I understand your question correctly, you are asking, what are generators and how are they used with associations?

The first answer is easy: Generators define a strategy for assigning ids to objects.

The second answer is: depends on how you set up your relationships. For example look at the documentation for a unique one-to-one based on primary key

http://docs.jboss.org/hibernate/core/3.5/reference/en/html/mapping.html#mapping-declaration-onetoone

here the generator class of one side is set to 'foreign', which is a special case because two tables are sharing the same id.

Note the other one-to-one example, in that case no generator is assigned on one side of the relationship.

If you are having issues with persistence I suggest the following 2 items

  1. Write integration-tests just for your DAOs. If you don't have DAOs, create them to make sure all this code is in one place.
  2. Look closely at the example code and tests that come with hibernate. This code is quite illuminating.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文